Intern
What is the value of NaN?
sobes.tech AI
Answer from AI
JavaScript primitive type representing a non-numeric value. It occurs when unsuccessful attempts are made to convert non-numeric values to numbers or when performing mathematical operations that do not result in a valid number.
Main properties:
typeof NaNequals"number".NaNis not equal to itself (NaN === NaNis false).- The only way to check for
NaNis to useisNaN()orNumber.isNaN().
Examples of obtaining NaN:
// unsuccessful conversion
Number('hello'); // NaN
// invalid mathematical operations
0 / 0; // NaN
Math.sqrt(-1); // NaN
// using NaN in operations
5 + NaN; // NaN
Examples of checking for NaN:
// incorrect check
NaN === NaN; // false
// correct check for global scope (may give false positives for non-numeric values)
isNaN(NaN); // true
isNaN('hello'); // true
// correct check for NaN (recommended)
Number.isNaN(NaN); // true
Number.isNaN('hello'); // false