Lesson 47 of 296
NaN and Object.is
What you will learn
- What NaN is and where it comes from
- Why NaN is not equal to itself
- The right way to check for NaN
isNaNvsNumber.isNaNObject.isand how it differs from===- Negative zero
On this page
What is NaN?
NaN means "Not a Number". It is the result of a maths operation that has no meaningful number answer. Strangely, its type is number:
typeof NaN "number"Where NaN comes from
Number("abc") NaN
parseInt("hello") NaN
0 / 0 NaN
Math.sqrt(-1) NaN
undefined + 1 NaN
"abc" * 3 NaNNaN spreads
Any maths with NaN gives NaN:
NaN + 5 NaN
NaN * 0 NaNSo one bad value early in a calculation turns the final result into NaN. When you see NaN, trace backwards to find the first bad value.
NaN is not equal to itself
NaN === NaN false
NaN == NaN false
NaN !== NaN trueNaN is the only value in JavaScript that is not equal to itself. So x === NaN is always false, even when x is NaN.
Also, every comparison with NaN is false:
NaN > 0 false
NaN < 0 false
NaN >= NaN falseChecking for NaN
Number.isNaN(value)Avoid the old global isNaN(). It converts the value to a number first, which gives confusing answers:
isNaN("hello") true ("hello" becomes NaN)
Number.isNaN("hello") false ("hello" is a string, not NaN)
isNaN(undefined) true
Number.isNaN(undefined) falseNumber.isNaN only returns true for the actual NaN value.
Checking for a valid number
Often what you really want to know is "is this a normal, usable number?":
Number.isFinite(value)It is false for NaN, Infinity, -Infinity, and anything that is not of type number.
Number.isFinite(42) true
Number.isFinite(NaN) false
Number.isFinite(Infinity) false
Number.isFinite("42") falseNumber.isInteger(5.0) is true; Number.isInteger(5.5) is false.
Negative zero
JavaScript has both 0 and -0.
-0 === 0 true
1 / 0 Infinity
1 / -0 -InfinityYou rarely care, but it can appear, for example Math.round(-0.4) is -0.
Object.is
Object.is(a, b) is a same-value check. It works like === with two differences:
=== | Object.is | |
|---|---|---|
NaN and NaN | false | true |
0 and -0 | true | false |
| Everything else | Same | Same |
Object.is(NaN, NaN) true
Object.is(0, -0) false
Object.is("a", "a") true
Object.is({}, {}) false (different objects)React uses Object.is to decide whether state has changed (Part 12). That is one practical reason to know it.
Array methods and NaN
[NaN].includes(NaN) true (includes uses same-value-zero)
[NaN].indexOf(NaN) -1 (indexOf uses ===)This is a known trick question: includes finds NaN, indexOf does not.
Common mistakes
if (x === NaN), which never works.- Using global
isNaNinstead ofNumber.isNaN. - Not checking user input, so NaN spreads through calculations.
How to say it in an interview
"NaN represents an invalid numeric result, and its type is number. It is the only value not equal to itself, so I check it with Number.isNaN, not with triple equals, and I avoid the global isNaN because it coerces first. Object.is is like triple equals except that it treats NaN as equal to NaN and distinguishes zero from negative zero. React uses Object.is to compare state."
Key words
| Word | Meaning |
|---|---|
| NaN | "Not a Number", the result of invalid maths |
Number.isNaN | True only for the real NaN value |
Number.isFinite | True only for normal, finite numbers |
Object.is | Same-value comparison |
| Negative zero | -0, equal to 0 with === but not with Object.is |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.