NaN is a reserved word in JavaScript that signifies an invalid numerical value.
Attempting arithmetic operations with a non-numeric string will yield NaN (Not a Number).
Example
let x = 100 / “Apple”; |
However, if the string contains numerical characters, the resultant value will be a valid number.
Example
let x = 100 / “10”; |
To ascertain whether a value is not a number in JavaScript, you can utilize the global function is NaN().
Example
let x = 100 / “Apple”; isNaN(x); |
Exercise caution when dealing with NaN. If NaN is employed in a mathematical operation, the resultant value will also be NaN.
Example
let x = NaN; let y = 5; let z = x + y; |
Alternatively, the outcome may be a concatenation, such as “NaN5”.
Example
let x = NaN; let y = “5”; let z = x + y; |
NaN is classified as a number; invoking typeof NaN will yield “number“.
Example
typeof NaN; |