Programming Search⌘K

Featurecore

NaN and Infinity

Also called NaN, not a number, Infinity, -Infinity, Number.isNaN, isNaN, overflow, division by zero.

NaN is the Number value for a failed or undefined result, and it is not equal to itself. Infinity comes from overflow and from division by zero. Number.isNaN tests for NaN with no conversion.

Language
JavaScript

Synopsis

Expression Result
Number('abc'), parseInt('blabla') NaN
Math.sqrt(-1) NaN
0 * Infinity, Infinity - Infinity NaN
NaN === NaN false
1 / 0, 10 ** 1000 Infinity
-1 / 0, Math.log(0) -Infinity
Number.isNaN('abc') false
isNaN('abc') true

The cases are from the MDN NaN and Infinity pages.1 2

console.log(parseInt("blabla"), Math.sqrt(-1), 0 * Infinity, 7 * "blabla");
console.log(NaN === NaN, Number.isNaN(NaN), typeof NaN);
console.log(1 / 0, -1 / 0, 10 ** 1000, Math.log(0), 1 / Infinity);
Output
NaN NaN NaN NaN
false true number
Infinity -Infinity Infinity -Infinity 0

Semantics

r1. NaN is a Number. ECMA-262 maps all the IEEE NaN values to "a single special NaN value". It adds that "to ECMAScript code, all NaN values are indistinguishable from each other."3 typeof NaN is 'number'. Example: synopsis.

r2. Where NaN comes from. MDN lists "five different types of operations that return NaN": a failed number conversion, a math result that "is not a real number", an indeterminate form such as 0 * Infinity, an operand that "is or gets coerced to NaN", and other invalid values shown as numbers.1 NaN spreads: "this means NaN is contagious".1 Example: synopsis.

r3. NaN compares false. "When NaN is one of the operands of any relational comparison ( > , < , >= , <= ), the result is always false . NaN compares unequal (via == , != , === , and !== ) to any other value", and that includes another NaN.1 "NaN is also one of the falsy values in JavaScript."1 Example: compare.

r4. Number.isNaN against isNaN. "As opposed to the global isNaN() function, the Number.isNaN() method doesn't force-convert the parameter to a number."4 The global isNaN() converts first, so isNaN('hello') is true. With null, '' and booleans, which convert to numbers, it is false. So it does not tell whether a value is the NaN value, and it does not tell whether a value is a number.5 Example: isnan.

r5. Self-comparison. "Since x !== x is only true for NaN among all possible JavaScript values, Number.isNaN(x) can also be replaced with a test for x !== x , despite the latter being less readable."4 Example: isnan.

r6. Searching for NaN. indexOf() uses strict equality, so "you cannot use indexOf(NaN) to find the index of a NaN value in an array".6 Same-value-zero treats NaN as equal to NaN, and "It's used by Array.prototype.includes() , TypedArray.prototype.includes() , as well as Map and Set methods".6 Example: compare.

r7. Infinity. "The value Infinity (positive infinity) is greater than any other number."2 "Positive values greater than Number.MAX_VALUE are converted to Infinity ."7 A nonzero number divided by 0 gives Infinity or -Infinity, and a number divided by Infinity gives 0.2 Example: synopsis.

Examples

The examples run with Node.js 24.

NaN in comparisons and searches (rules r3 and r6):

console.log(NaN < 1, NaN >= 1, NaN == NaN, NaN != NaN, Boolean(NaN));
const list = [1, NaN, 3];
console.log(list.indexOf(NaN), list.includes(NaN), new Set([NaN, NaN]).size);
Output
false false false true false
-1 true 1

The two NaN tests (rules r4 and r5):

const inputs = [NaN, "hello", "", null, true, "12", undefined];
console.log(inputs.map((v) => isNaN(v)).join(" "));
console.log(inputs.map((v) => Number.isNaN(v)).join(" "));
const x = 0 / 0;
console.log(x !== x);
Output
true true false false false false true
true false false false false false false
true

Pitfalls

x === NaN is always false

Use Number.isNaN(x). A test x === NaN never passes, even when x is NaN (rule r3).4

The global isNaN converts

isNaN('hello') is true and isNaN('') is false (isnan). To test that a value is a number, MDN says to use typeof x === 'number'.5

One NaN spoils a sum

A single NaN in a list makes the total NaN (rule r2). Check the inputs, or the total, with Number.isNaN.

Comparison

Topic JavaScript Python
make NaN or infinity NaN, Infinity globals1 2 float('nan'), float('inf'), from the nan and infinity input forms8
1 / 0 Infinity2 "Division by zero raises the ZeroDivisionError exception."9

JavaScript gives Infinity where Python raises an error. See The float type.

References

  1. NaN, MDN, Description; Testing against NaN
  2. Infinity, MDN, Description; Using Infinity
  3. ECMAScript 2026 Language Specification, 6.1.6.1 The Number Type
  4. Number.isNaN(), MDN, Description
  5. isNaN(), MDN, Description
  6. Equality comparisons and sameness, MDN, Strict equality using ===; Same-value-zero equality
  7. JavaScript data types and data structures, MDN, Number type
  8. Built-in Functions, The Python Standard Library, float()
  9. Expressions, The Python Language Reference, 6.7 Binary arithmetic operations

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source