How to check if an object is null or undefined in JavaScript:
This post will show you how to check if an object is null or undefined in JavaScript. You might want to check if an object has data or not before accessing any items of it. If it is null or undefined, it will break your code.
Let’s learn how to do that.
undefined and null:
undefined and null keywords have different meanings in JavaScript. null is one of the primitive value in JavaScript. It indicates the absense of a value. But, undefined means it is not defined. If something is undefined, it indicates that no value is assigned to it.
We can assign null to a variable but if a variable is undefined, it means that it is declared but nothing is assigned to it.
We can either use equality operator or strict equality operator to check if something is undefined or null.
Example on undefined and null:
Let’s take a look at the below example:
let o1;
let o2 = undefined;
let o3 = null;
let o4 = {};
let o5 = 12;
console.log('== null:');
console.log(o1 == null);
console.log(o2 == null);
console.log(o3 == null);
console.log(o4 == null);
console.log(o5 == null);
console.log('== undefined:');
console.log(o1 == undefined);
console.log(o2 == undefined);
console.log(o3 == undefined);
console.log(o4 == undefined);
console.log(o5 == undefined);
In this example, we created 5 different object variables and assign different values to these variables.
The log statements are printing the value of each variable with equality operator with null and undefined.
It will print:
== null:
true
true
true
false
false
== undefined:
true
true
true
false
false
As you can see, it printed true if we are comparing with null or undefined because null == undefined results true.
Now, let’s try it with ===:
let o1;
let o2 = undefined;
let o3 = null;
let o4 = {};
let o5 = 12;
console.log('=== null:');
console.log(o1 === null);
console.log(o2 === null);
console.log(o3 === null);
console.log(o4 === null);
console.log(o5 === null);
console.log('=== undefined:');
console.log(o1 === undefined);
console.log(o2 === undefined);
console.log(o3 === undefined);
console.log(o4 === undefined);
console.log(o5 === undefined);
It will print:
=== null:
false
false
true
false
false
=== undefined:
true
true
false
false
false
So, if we use ===, we have to check for both undefined and null.
Method 1: By using equality operator:
We can use equlity operator, == with null or undefined to check if an object is either null or undefined. Below is the complete program:
const isNullOrUndefined = o => o == null;
let o1;
let o2 = undefined;
let o3 = null;
let o4 = {};
let o5 = 12;
if(isNullOrUndefined(o1)){
console.log('o1 is null or undefined');
}
if(isNullOrUndefined(o2)){
console.log('o2 is null or undefined');
}
if(isNullOrUndefined(o3)){
console.log('o3 is null or undefined');
}
if(isNullOrUndefined(o4)){
console.log('o4 is null or undefined');
}
if(isNullOrUndefined(o5)){
console.log('o5 is null or undefined');
}
It will print:
o1 is null or undefined
o2 is null or undefined
o3 is null or undefined
So, we can simply use == to compare the value with null or undefined.
Method 2: By using strict equality operator:
We can also use strict equality operator or === to check for both null and undefined.
const isNullOrUndefined = o => o === null || o === undefined;
let o1;
let o2 = undefined;
let o3 = null;
let o4 = {};
let o5 = 12;
if(isNullOrUndefined(o1)){
console.log('o1 is null or undefined');
}
if(isNullOrUndefined(o2)){
console.log('o2 is null or undefined');
}
if(isNullOrUndefined(o3)){
console.log('o3 is null or undefined');
}
if(isNullOrUndefined(o4)){
console.log('o4 is null or undefined');
}
if(isNullOrUndefined(o5)){
console.log('o5 is null or undefined');
}
It will print the same result.
o1 is null or undefined
o2 is null or undefined
o3 is null or undefined
Method 3: Using typeof:
You can also use typeof to filter out uninitialize variables. For example, if you use any variable which is not initialized, it will throw an exception. This can be fixed with typeof.
let o1;
let o2 = undefined;
let o3 = null;
let o4 = {};
let o5 = 12;
console.log(typeof(o1));
console.log(typeof(o2));
console.log(typeof(o3));
console.log(typeof(o4));
console.log(typeof(o5));
console.log(typeof(o6));
It will print:
undefined
undefined
object
object
number
undefined
o6 is not defined. And typeof returns undefined for this. But, for null, it returns object. So, even if you use typeof, make sure to check if it is null or not.
You might also like:
- JavaScript string codePointAt method explanation with example
- 3 ways in JavaScript to remove all hyphens from a string
- How to remove all zeros from a number string in JavaScript
- 6 different ways in JavaScript to print the content of an array
- JavaScript array values() function
- 3 ways to get a random value from an array in JavaScript
- 4 ways in JavaScript to check if a string starts with a number