null vs undefined vs NaN in JavaScript
What’s the difference between null, undefined and NaN in JavaScript?
null
A null value represents nothing, nonexistent or invalid object or address. It converts to 0
in simple arithmetic operations and it’s global object. null == false
gives us false.
undefined
The global undefined property represents the primitive value undefined
. It tells us something has not assigned value; isn’t defined. undefined isn’t converted into any number, so using it in maths calculations returns NaN
.
NaN
NaN (Not-A-Number) represents something which is not a number, even though it’s actually a number. It’s not equal to itself and to check if something is NaN we need to use isNaN() function.