July 14, 2019

Difference between null and undefined in JavaScript

null and undefined are similar and both mean an absence of value. They are considered equal by the equality operator (==).
console.log(null == undefined); // true

To distinguish them the strict equality operator (===) should be used.
console.log(null === undefined); // false

The main difference between them is that undefined represents an unexpected absence of value and the value of a variable should not be set to undefined. null, however, represents normal, expected absence of value and if there is no value, the value of the variable can be set to null.

No comments:

Post a Comment