ScanSkill

NaN

NaN is a property representing Not-A-Number.

Syntax

NaN

Example

const a = NaN, b = NaN;
console.log(a === b); // Prints: false

console.log(isNaN(a)) // Prints: true

const number = parseInt("asdasd");
console.log(number); // Prints: NaN

NaN is a property of the global object. To check if the variable is not a number, isNaN() function is used. NaN is received because of following cases:

  • The number cannot be parsed (e.g. parseInt("something"))
  • Math operation where the result is not a real number (e.g. Math.sqrt(-1))
  • Operand of an argument is NaN (e.g. 43 + NaN)
  • Indeterminate form (e.g. 0 * Infinity, or undefined + undefined)
  • Any operation that involves a string and is not an addition operation (e.g. "foo" / 3)