By Object.is() function

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
 
    // The following is equivalent to
    // maybeNull == null
    // or maybeNull == undefined:
    console.log(Object.is(maybeNull, undefined)
        || Object.is(maybeNull, null))
         
    // Compare to the following:
    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

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