AppDividend
Latest Code Tutorials

Javascript Null Check: How to Check Null using Object.is()

0

To check what is the data type of variable in JavaScript, use the typeof operator. The typeof is a unary operator returns a string indicating the type of the unevaluated operand. The null value describes the intended absence of object value. To check the typeof null, use the following code.

// app.js

data = null

console.log(typeof(data))

Output

object

You can see that null is an object in JavaScript. But null is primitive value in JavaScript like String, Number, Boolean, undefined, and Symbol. Regrettably, if you call typeof operator on null values, it returns “object“.

JavaScript null is a primitive value, but its data type is an object. Now, this is a historical bug in JavaScript from the beginning that has never been fixed by developers.

The null is a falsy value. What I mean by that it takes as false if constrained to a boolean.

data = null

if (data) {
  console.log('The null value is not falsy')
}
else {
  console.log('The null value is falsy')
}

Output

The null value is falsy

That means if you want to check for a null value, then if-else statement might not be helpful for you because it returns falsy results that can lead you to miscalculation.

So, using the typeof operator to check for a “null” value does no good and will lead to the wrong path.

How to check a null value in JavaScript

To check a null value in JavaScript, use equality operators like == or ===. The null value is not loosely equal to any of the other falsy values except undefined and null itself.

data = null

console.log("The value of data is: ", data)
console.log('data == 0', data == 0)
console.log('data == on', data == 0n)
console.log('data==data', data == data)
console.log('data==undefined', data == undefined)
console.log('data==false', data == false)
console.log('data==NaN', data == NaN)
console.log('data==""', data == "")

Output

The value of data is:  null
data == 0 false
data == on false
data==data true
data==undefined true
data==false false
data==NaN false
data=="" false

From the output, you can see that null is only loosely equal to undefined and itself. It is not equal to any other falsy values.

So, when you come across a use case in which you want to check if the variable has any value at all before processing that value, use == null to check for either null or undefined.

JavaScript null check: Strict equality using ===

To strictly check the null value in JavaScript, use the triple equality operator(===).

data = null

console.log("The value of data is: ", data)
console.log('data === 0', data === 0)
console.log('data === on', data === 0n)
console.log('data === data', data === data)
console.log('data === undefined', data === undefined)
console.log('data === false', data === false)
console.log('data === NaN', data === NaN)
console.log('data=== ""', data === "")

Output

The value of data is:  null
data === 0 false
data === on false
data === data true
data === undefined false
data === false false
data === NaN false
data=== "" false

You can see that if you check null === undefined, it returns false. That means, if we strictly check both values, then they are not the same. So, if you are explicitly checking null value, then use the triple equality operator(===). It is the recommended operator to check null values in JavaScript.

To explicitly check if the value is null, use the triple equality operator. There are many JavaScript developers who favor everything to be explicit for accuracy, including me, and there is nothing wrong with that.

JavaScript Object.is() method to check null value

Javascript Object is() an inbuilt method that checks whether two values are the same value. The ES2015 method Object.is() differs from the strict === and loose == equality operators in how it checks for NaN and negative zero -0 values.

To check null values, the behavior of Object.is() is the same as (===) triple equality operator.

data = null

console.log("The value of data is: ", data)
console.log('If data is strictly equal to undefined', Object.is(data, undefined))
console.log('If data is strictly equal to data', Object.is(data, data))

Output

The value of data is:  null
If data is strictly equal to undefined false
If data is strictly equal to data true

If you want to check explicitly about if the variable is null or undefined, then use JavaScript Object.is() method. The Object.is() method is same as === when it comes to compare null and undefined.

Conclusion

To check null in JavaScript, use triple equals operator(===) or Object.is() method. To find the difference between null and undefined, use the triple equality operator or Object.is() method.

To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same. So if you want to strict check, then don’t use a double equality operator. It will lead to misconclusion.

The best way to check for a “null” value in JavaScript is to do a strict equality comparison of the value against the null keyword or Object.is() method and pass both values.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.