Input validation
Validate parsed or calculated numeric input before further arithmetic.
JS Puzzles · Const
NaN is unequal to every value, including itself, under == and ===. Detect it with Number.isNaN() rather than an equality comparison.
NaN is a numeric value produced by invalid or undefined numerical operations. Unlike ordinary values, it is unequal to every value, including itself, when tested with == or ===; use Number.isNaN() to check for it, while Object.is() considers NaN equal to itself.
Number.isNaN(value)| Name | Type | Description |
|---|---|---|
value | any | The value to test for the numeric NaN value. |
A Boolean indicating whether the supplied value is NaN.
const value = Number("not a number");
console.log(value === value); // false
console.log(Number.isNaN(value)); // true
console.log(Object.is(value, value)); // trueThe snippet demonstrates that strict equality does not identify NaN, while Number.isNaN does and Object.is treats the two NaN values as the same.
Validate parsed or calculated numeric input before further arithmetic.
Identify invalid numeric results produced by conversions or mathematical operations.
Filter or classify numeric data containing invalid values.
Number.isNaN()isNaN()Object.is()strict equality (===)loose equality (==)SameValueZero