Skip to content

null vs undefined vs NaN in JavaScript

What’s the difference between null, undefined and NaN in JavaScript?

null

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.

See also  Replace all instances of a string in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

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