DEV Community

loading...

Nothing is as it seems...

aidanlincoln profile image AidanLincoln ・1 min read

Today I learned that NaN (not a number) is actually a number, and null (no value) is an object in JavaScript.

NaN is a numeric datatype, but its value cannot be expressed with real numbers. It is a non-configurable, non-writable property. The name "Not a Number" doesn't mean "This value isn't numeric", it just means "I can't process this, so I'm going to tell you it's not a valid number". JavaScript's number datatype is based on the floating point standard (IEEE 754 - https://standards.ieee.org/standard/754-2019.html), which defines rules for a number's storage in memory, what operations return, and what a comparison returns. If any of the values you are comparing are NaN, the value returned will be false.

Examples:

typeof(NaN) // "number"

const var1 = 5 * "abc"
const var2 = 5 * "abc"

var1 // NaN
var2 // NaN

var1 == var2 // false

NaN == NaN // false

The value null represents the absence of any object value, but oddly enough, it is an object according to JavaScript. This is often regarded as a mistake from the first version of JavaScript that can't be fixed, but some disagree contending that it was not a mistake. Very few people believe it makes complete sense, though. Lots of code is reliant on null being an object, so 'fixing' it would result in many additional bugs. If it ain't broke, don't fix it?

Example:

typeof null // "object"

Discussion (1)

pic
Editor guide
Collapse
bitbrush profile image
bitbrush

Yeah, don't fix it! It's working as is!