null is used for representing the intentional absence of an object value and is a primitive value. Unlike undefined, it is not a property of the global object.
It is equal to undefined but not identical to it.
CAREFUL: The typeof null is 'object'.
To properly check if a value is null, compare it with the strict equality operator
It is a property of the global object, and a reference to Number.NaN
Perhaps confusingly, NaN is still considered a number.
Don't check for NaN using the equality operator. See isNaN instead.
undefined and null
At first glance it may appear that null and undefined are basically the same, however there are subtle but important differences.
undefined is the absence of a value in the compiler, because where it should be a value, there hasn't been put one, like the case of an unassigned variable.
- undefined is a global value that represents the absence of an assigned value.
typeof undefined === 'undefined'
- null is an object that indicates that a variable has been explicitly assigned "no Value".
typeof null === 'object'
Setting a variable to undefined means the variable effectively does not exist. Some processes, such as JSON serialization, may strip undefined properties from objects. In contrast, null properties indicate will be preserved so you can explicitly convey the concept of an "empty" property.
The following evaluate to undefined:
undefined is also a property of the global window object.
Before ECMAScript 5 you could actually change the value of the window.undefined property to any other value potentially breaking everything.
The Number constructor has some built in constants that can be useful
Math library functions that return NaN
Generally, Math functions that are given non-numeric arguments will return NaN.
The square root of a negative number returns NaN, because Math.sqrt does not support imaginary or complex numbers.