Infinity, NaN and Negative Zero in JavaScript: Special Number Values Explained
Why dividing by zero doesn't crash JavaScript, where NaN comes from and why NaN !== NaN, isNaN vs Number.isNaN, negative zero and how to detect it, the Number constants (MAX_VALUE, MIN_VALUE, EPSILON, safe integers), and the three equality algorithms ===, Object.is and SameValueZero.
kvi5hnuCode Sprout team member7 min readOn this page
In many languages, dividing by zero or taking the square root of a negative number throws an error. JavaScript's numbers follow the IEEE 754 standard instead, which has special values for these situations: Infinity, -Infinity, NaN (not a number), and even a negative zero. Arithmetic in JavaScript never throws; it produces one of these values and carries on.
That's convenient, but it means a bug can quietly turn your data into NaN several steps before you notice. This article explains where each special value comes from, how to detect it, and the surprising equality rules around them.

Infinity: overflow and division by zero
A result too large to represent becomes Infinity (or -Infinity), and so does dividing a non-zero number by zero:
console.log(Number.MAX_VALUE * 2); // overflow
console.log(-Number.MAX_VALUE * 2);
console.log(10 ** 400);
console.log(1 / 0, -1 / 0); // division by zero is not an error
console.log(Infinity > Number.MAX_VALUE, -Infinity < -1e308);
Infinity
-Infinity
Infinity
Infinity -Infinity
true true
Infinities behave the way mathematics suggests, until a result has no meaningful value:
| Expression | Result | Why |
|---|---|---|
Infinity + 1, Infinity * 2 | Infinity | still unbounded |
1 / Infinity | 0 | approaches zero |
-1 / Infinity | -0 | approaches zero from below |
Infinity - Infinity | NaN | undefined in mathematics |
Infinity * 0 | NaN | undefined |
Infinity / Infinity | NaN | undefined |
The global constants Infinity and NaN are the same values as Number.POSITIVE_INFINITY and Number.NaN.
The opposite of overflow is underflow: a result closer to zero than the smallest representable number becomes 0, or -0 if it was negative. Number.MIN_VALUE / 2 is 0.
NaN: "not a number"
NaN is a value of the number type (typeof NaN is "number") that means "the result of this numeric operation is undefined or unrepresentable". You get it from:
console.log(0 / 0); // undefined math
console.log(Math.sqrt(-1)); // no real square root
console.log(Number("abc")); // a string that isn't a number
console.log(parseInt("px12")); // parsing that finds no digits at the start
console.log(undefined + 1); // undefined converts to NaN
console.log(NaN + 5, NaN * 0); // NaN spreads through every calculation
console.log(Math.max(4, NaN));
NaN
NaN
NaN
NaN
NaN
NaN NaN
NaN
Because NaN propagates, one bad input can turn a whole chain of calculations into NaN. The typical source in real code is undefined sneaking in: a missing property (order.qty when the field is called quantity) or a missing argument.
NaN is not equal to itself
NaN is the only JavaScript value that isn't equal to itself:
const result = 0 / 0;
console.log(result === NaN); // never true, even for NaN
console.log(result !== result); // the classic self-comparison trick
console.log(Number.isNaN(result)); // the clear way to test
console.log(Object.is(result, NaN));
false
true
true
true
The IEEE standard defines it this way because two NaNs usually come from different undefined computations, and claiming they're equal would be misleading. The practical consequence: never test for NaN with === or ==. Use Number.isNaN(x).
isNaN vs Number.isNaN
There are two functions, and the old one is misleading:
for (const value of [NaN, "abc", undefined, "", " ", "42", {}]) {
console.log(String(JSON.stringify(value)).padEnd(10), isNaN(value), Number.isNaN(value));
}
null true true
"abc" true false
undefined true false
"" false false
" " false false
"42" false false
{} true false
(JSON.stringify(NaN) is null, which is why the first row says null.)
The global isNaN(x) first converts its argument to a number, then checks. So it answers "would this become NaN as a number?", which is true for "abc", undefined and {}. Number.isNaN(x) (ES2015) answers the real question, "is this value NaN?", without converting. Prefer Number.isNaN.
The same split exists for finiteness: the global isFinite("42") is true because it converts the string, while Number.isFinite("42") is false because a string isn't a number. Number.isFinite(x) is the best single check for "a usable number": it's false for NaN, Infinity, -Infinity and every non-number.
Negative zero
IEEE 754 numbers store the sign separately, so there are two zeros: 0 and -0. They're almost indistinguishable:
const negZero = -0;
console.log(0 === negZero, 0 == negZero); // they compare equal
console.log(Object.is(0, negZero)); // but they are different values
console.log(1 / negZero); // the difference shows when dividing
console.log(String(negZero), JSON.stringify(negZero), `${negZero}`); // most conversions hide the sign
console.log(negZero); // console.log in Node shows it
console.log(0 * -1, Math.round(-0.4), -1 / Infinity); // ways to produce -0
true true
false
-Infinity
0 0 0
-0
-0 -0 -0
Converting to a string drops the sign, so a -0 shows up as 0 almost everywhere, which is usually what you want. Where it matters is when the sign carries meaning: in physics, graphics and animation code, a velocity of -0 means "stopped, while moving left", and Math.atan2(0, -0) is π while Math.atan2(0, 0) is 0. To detect negative zero, use Object.is(x, -0), or the older trick x === 0 && 1 / x === -Infinity.
The Number constants and checks
| Name | Value | Meaning |
|---|---|---|
Number.MAX_VALUE | about 1.79 × 10^308 | the largest finite number |
Number.MIN_VALUE | 5 × 10^-324 | the smallest positive number, not the most negative! |
Number.MAX_SAFE_INTEGER | 2^53 - 1 = 9007199254740991 | largest integer n such that n and n + 1 are both exact |
Number.MIN_SAFE_INTEGER | -(2^53 - 1) | its negative |
Number.EPSILON | 2^-52, about 2.22 × 10^-16 | the gap between 1 and the next larger number |
Number.POSITIVE_INFINITY, NEGATIVE_INFINITY, NaN | the special values |
And the checks, none of which convert their argument:
| Check | True for |
|---|---|
Number.isNaN(x) | NaN only |
Number.isFinite(x) | any number except NaN and ±Infinity |
Number.isInteger(x) | finite numbers with no fraction (5, 5.0, -0) |
Number.isSafeInteger(x) | integers within ±(2^53 - 1) |
const big = Number.MAX_SAFE_INTEGER;
console.log(big, big + 1, big + 2); // big + 2 can't be represented
console.log(2 ** 53 === 2 ** 53 + 1); // two different integers compare equal!
console.log(Number.isSafeInteger(2 ** 53 - 1), Number.isSafeInteger(2 ** 53));
9007199254740991 9007199254740992 9007199254740992
true
true false
Above 2^53, not every integer can be stored, so 2 ** 53 + 1 rounds to 2 ** 53. This is why database IDs and other 64-bit integers from APIs are often sent as strings in JSON: as JavaScript numbers they'd silently lose their last digits. Use bigint for such values (article 13).
Three kinds of equality
Special values are where JavaScript's equality algorithms differ:
| Comparison | NaN vs NaN | 0 vs -0 | Used by |
|---|---|---|---|
=== (strict equality) | false | true | your code, indexOf, lastIndexOf |
Object.is (SameValue) | true | false | Object.is, Object.defineProperty checks |
| SameValueZero | true | true | includes, Map keys, Set values |
const values = [NaN, 0];
console.log(values.indexOf(NaN)); // === can't find NaN
console.log(values.includes(NaN)); // SameValueZero can
console.log(values.includes(-0)); // and treats -0 like 0
console.log(new Set([NaN, NaN, 0, -0]).size); // one NaN, one zero
-1
true
true
2
This is why includes is the better way to search an array for NaN, and why a Set never holds two NaNs.
Common questions
Why does JavaScript return Infinity instead of throwing on division by zero?
JavaScript numbers follow the IEEE 754 standard, which defines division by zero as ±Infinity and 0/0 as NaN so that long calculations can continue and be checked at the end. Integer types in other languages throw because they have no way to represent infinity. JavaScript's bigint does throw: 1n / 0n is a RangeError.
How do I stop NaN from spreading through my calculations?
Validate inputs at the boundary, where data enters your program: after parsing user input, reading JSON or calling an API, check Number.isFinite(value) and handle the bad case right there. Checking only at the end tells you that something went wrong, but not where.
Is typeof NaN === "number" a bug?
No, it's by design. NaN is a special value of the IEEE 754 number format, the result of a numeric operation. "Not a number" describes what it represents, not its type.
Is NaN truthy or falsy?
Falsy. if (NaN) doesn't run, and NaN || 5 is 5. So is -0. Infinity and -Infinity are truthy.
Summary
- Arithmetic never throws: overflow and division of non-zero numbers by zero give ±
Infinity, and undefined operations giveNaN. NaNpropagates through calculations and is not equal to itself. Test withNumber.isNaN, not the converting globalisNaN.Number.isFinite(x)is the best "is this a usable number?" check.-0 === 0, butObject.isand1 / xcan tell them apart. Most string conversions hide the sign.- Integers are exact only up to 2^53 - 1 (
Number.MAX_SAFE_INTEGER).Number.MIN_VALUEis the smallest positive number. ===,Object.isand SameValueZero (includes,Map,Set) differ only forNaNand ±0.
Practice Questions
What do [NaN].indexOf(NaN) and [NaN].includes(NaN) return, and why do they differ?
Answer
-1 and true. indexOf compares with ===, under which NaN equals nothing. includes uses SameValueZero, which treats NaN as equal to NaN.
A function computes total / count and sometimes returns NaN, sometimes Infinity. What inputs cause each?
Answer
Infinity (or -Infinity) when count is 0 and total is non-zero. NaN when both are 0, or when either is NaN, or when either is something that converts to NaN, such as undefined.
What does isNaN("hello") return, and what does Number.isNaN("hello") return?
Answer
true and false. The global isNaN converts "hello" to a number first (NaN), while Number.isNaN sees a string, which isn't the NaN value.
Is Number.MIN_VALUE < 0?
Answer
No. Number.MIN_VALUE is the smallest positive number, about 5 × 10^-324. The most negative finite number is -Number.MAX_VALUE.
Write a function isNegativeZero(x) without using Object.is.
Answer
const isNegativeZero = x => x === 0 && 1 / x === -Infinity;
Only -0 is equal to 0 and gives -Infinity when used as a divisor.
Previous: JavaScript Numbers: Integer and Floating-Point Literals and the Math Object
Next: Why 0.1 + 0.2 !== 0.3 in JavaScript: Floating-Point Precision, Rounding and Money

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.