HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

What is the only value not equal to itself in JavaScript?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptwhatequalvaluenottheitselfonly

Problem

NaN (Not-a-Number) is the only JavaScript value not equal to itself when comparing with any of the comparison operators. NaN is often the result of meaningless or invalid math computations, so it doesn't make sense for two NaN values to be considered equal.
You can check for NaN values using the Number.isNaN() function. Note that this is different from the original , global isNaN(). Their difference lies in the fact that isNaN() forcefully converts its argument to a number, whereas Number.isNaN() doesn't. This is why Number.isNaN() is considered more robust and preferable in most cases.

Solution

const x = Math.sqrt(-1); // NaN
const y = 0 / 0;         // NaN

x === y;                 // false
x === NaN;               // false

Number.isNaN(x);         // true
Number.isNaN(y);         // true

isNaN(x);                // true
isNan('hello');          // true

Code Snippets

const x = Math.sqrt(-1); // NaN
const y = 0 / 0;         // NaN

x === y;                 // false
x === NaN;               // false

Number.isNaN(x);         // true
Number.isNaN(y);         // true

isNaN(x);                // true
isNan('hello');          // true

Context

From 30-seconds-of-code: value-not-equal-to-itself

Revisions (0)

No revisions yet.