How to check for null values in JavaScript ?
Last Updated :
15 Jan, 2024
The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is because a variable that has been declared but not assigned any value is undefined, not null.
Below are the approaches:
By this operator, we will learn how to check for null values in JavaScript by the (===) operator. This operator only passes for null values, not for undefined, false, 0, NaN.
Syntax:
x === y;
Example: The following code snippets show some comparison of objects.
Javascript
const object1 = {
key: "value" ,
};
const object2 = {
key: "value" ,
};
console.log(object1 === object2);
console.log(object1 === object1);
|
This function checks whether two objects’ values are equal or not. If they are the same the two object’s values are the same if both values are null.
Syntax:
Object.is( a, null )'
Example: In this example, we are using Object.is() function.
Javascript
let maybeNull = null
console.log(Object.is(maybeNull, undefined)
|| Object.is(maybeNull, null ))
console.log(maybeNull == null )
console.log(maybeNull == undefined)
console.log(maybeNull === null )
console.log(Object.is(maybeNull, null ))
console.log(maybeNull === undefined)
console.log(Object.is(maybeNull, undefined))
maybeNull = undefined
console.log(maybeNull === undefined || maybeNull === null )
console.log(maybeNull == null )
console.log(maybeNull == undefined)
console.log(maybeNull === null )
console.log(Object.is(maybeNull, null ))
console.log(maybeNull === undefined)
console.log(Object.is(maybeNull, undefined))
|
Output
true
true
true
true
true
false
false
true
true
true
false
false
true
true
Using typeof operator is very useful because if a variable is not declared, it will show ReferenceError. But, the typeof a non-declared value is undefined, so using the typeof is a better way to check for null and undefined variables.
Syntax:
typeof var;
Example: In this example, we are using typeof Operator.
Javascript
console.log( typeof undeclaredVariable !== "undefined"
&& ( typeof undeclaredVariable !== "object"
|| !undeclaredVariable))
try { undeclaredVariable == null }
catch (e) { console.log(e) }
try { undeclaredVariable === null }
catch (e) { console.log(e) }
try { undeclaredVariable === undefined }
catch (e) { console.log(e) }
let declaredButUndefinedVariable
console.log( typeof declaredButUndefinedVariable
!== "undefined" &&
( typeof declaredButUndefinedVariable !== "object"
|| !declaredButUndefinedVariable))
let stringVariable = "Here's Johnny!"
console.log( typeof stringVariable !== "undefined" &&
( typeof stringVariable !== "object" || !stringVariable))
const isNotNullNorUndefined =
(value) => typeof value !== "undefined"
&& ( typeof value !== "object" || !value)
console.log(isNotNullNorUndefined(stringVariable))
|
Output
...js:7:7)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47
ReferenceError: undeclaredVariable is not defined
at Object.<anonymous> (/home/guest/sandbox/55da4d93-5c09-4cd3-8be9-a1459e8ed1b0.js:8:7)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47
false
true
true
Lodash _.isNull() method is used to find whether the value of the object is null. If the value is null then returns true otherwise it returns false.
Syntax:
_.isNull(value);
Example: In this example, we are checking whether the given value is null or not by the use of the _isNull() method.
Javascript
const _ = require( "lodash" );
let gfg = _.isNull( null );
let gfg1 = _.isNull(void 0);
console.log(gfg);
console.log(gfg1);
|
Output:
true
false
Share your thoughts in the comments
Please Login to comment...