By equality Operator (===)

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);


Output

false
true

How to check for null values in JavaScript ?

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:

Table of Content

  • By equality Operator (===)
  • By Object.is() function
  • By the typeof Operator
  • Using Lodash _.isNull() Method

Similar Reads

Approach 1: By equality Operator (===)

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....

Approach 2: By Object.is() function

...

Approach 3: By the typeof Operator

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....

Approach 4: Using Lodash _.isNull() Method

...

Contact Us