Sobes.tech
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 NaN equals "number".
  • NaN is not equal to itself (NaN === NaN is false).
  • The only way to check for NaN is to use isNaN() or Number.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
What is the value of NaN? — Node.js - sobes.tech