Javascript null check

Active3 hr before
Viewed126 times

9 Answers


How can I check for null values in JavaScript? I wrote the code below but it didn't work., Meta Stack Overflow , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Stack Overflow help chat

Javascript is very flexible with regards to checking for "null" values. I'm guessing you're actually looking for empty strings, in which case this simpler code will work:

if (!pass || !cpass || !email || !cemail || !user) {
load more v

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations. ,JavaScript data structures,Warning: JavaScript 1.6's for-each-in loops are deprecated,A re-introduction to JavaScript

load more v

The JavaScript primitive type null represents an intentional absence of a value — it is usually set on purpose to indicate that a variable has been declared but not yet assigned any value.,“The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.” — MDN Docs,This contrasts null from the similar primitive value undefined , which is an unintentional absence of any object value.,Generally, it is a good idea to catch both null and undefined values, as both represent an absence of a value.

load more v

JavaScript null is a primitive type that contains a special value null.,JavaScript uses the null value to represent the intentional absence of any object value.,Javascript null is a primitive type that has one value null.,JavaScript uses the null value to represent a missing object.

The following example defines the Circle class whose constructor accepts an argument radius. The Circle class has a static method called create() that returns a new Circle object with a specified radius.

.wp - block - code {
      border: 0;
      padding: 0;

   .wp - block - code > div {
      overflow: auto;

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;

   .hljs {
      box - sizing: border - box;

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
class Circle {
   constructor(radius) {
      this.radius = radius;
   get area() {
      return Math.PI * Math.pow(this.radius, 2);

   static create(radius) {
      return radius > 0 ? new Circle(radius) : null;
Code language: JavaScript(javascript)
load more v

null and undefined are primitive values in JavaScript. ,An undefined value means lack of value. ,A null value means absence., JavaScript - null and undefined

var myVar = null;

alert(myVar); // null 
load more v

missingObject === null evaluates to true because missingObject variable contains a null value.,If the variable contains a non-null value, like an object, the expression existingObject === null evaluates to false.,null is a special value in JavaScript that represents a missing object.,Hm… how could the type of a missing object evaluate to 'object'? Turns out typoef null being 'object' was a mistake in the early JavaScript implementation.

let myObject = {
   name: 'Eric Cartman'
load more v

There’s a long-standing bug in JavaScript meaning that every check for an object requires a check for null: typeof null === "object".,And whether you check object !== null or object != null ends up being your personal preference when type checking JavaScript objects.,Yes, it’s a little absurd, but you have to check for null when you check for objects in vanilla JavaScript. C’est la vie.,Really, though, you only need to check for null check for a JavaScript object. It’s the ol’ JavaScript Object Null Check™ (since 1995).

load more v

The typeof operator for undefined value returns undefined. Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator.,Note: We cannot use the typeof operator for null as it returns object.,To understand this example, you should have the knowledge of the following JavaScript programming topics:,JavaScript typeof Operator

Example 1: Check undefined or null

// program to check if a variable is undefined or null

function checkVariable(variable) {

   if (variable == null) {
      console.log('The variable is undefined or null');
   } else {
      console.log('The variable is neither undefined nor null');

let newVariable;

load more v

You can easily check if a variable Is Null or Not Null in JavaScript by applying simple if-else condition to the given variable. ,How to create a link in JavaScript ?,Check if an array is empty or not in JavaScript,How to remove a character from string in JavaScript ?


if (my_var) {


if (my_var !== null) {
load more v

Other "check-javascript" queries related to "Javascript null check"