JS: NaN
Meaning of NaN
NaN
means Not a Number. It is typically returned when converting a value to number that doesn't make sense.
// convert string to number const xx = Number("xyz"); // if arg does not make sense, result is NaN console.log(Number.isNaN(xx));
NaN is a Property
NaN
is the value of the property key "NaN"
of
the Global Object.
console.log(globalThis.hasOwnProperty("NaN")); console.log(Number.isNaN(globalThis["NaN"]));
NaN is a Literal Expression
NaN
is a literal value. For example, you can write let x = NaN;
〔see Number Constructor〕
Type of NaN
Type of NaN
is "number"
.
typeof NaN === "number"
Test Equality of NaN
NaN === NaN
return false
.
(<span class="constant">NaN</span> === <span class="constant">NaN</span>) === <span class="constant">false</span>
To test equality of NaN
, use
Object.is
.
To test if a value is NaN
, use
Number.isNaN.