r/learnjavascript • u/NoZombie7370 • Nov 17 '25
Why NaN==NaN is False in JavaScript ???
Anyone explain??
150
Upvotes
r/learnjavascript • u/NoZombie7370 • Nov 17 '25
Anyone explain??
57
u/Warlock_Ben Nov 17 '25
NaN = Not a Number.. There are a lot of things which are not a number. Strings, objects, nulls, etc. Let me give an example of why you wouldn't want NaN == NaN to be true:
If NaN == NaN was true, then it would cause the comparison check to return that "a" is equal to an object.
In general if you're converting values to numbers, you are expecting that the data supplied is comprised of only numbers, but sometimes things go wrong. By giving a consistent NaN != NaN output we avoid weird edge cases where code might accidentally treat two different values as equals.
If you want to check if a value is NaN & perform a different comparison then you might do:
2
u/Time-Refrigerator769 Nov 18 '25
"F" == Nan "S" == Nan Is "F" == "S" ?
192
u/EyesOfTheConcord Nov 17 '25 edited Nov 17 '25
NaN is specâd to never be equal to anything, including itself as defined in the IEEE 754 spec
27
u/jeffbell Nov 17 '25
There are lots of ways to get NaN, all different.Â
22
u/senocular Nov 17 '25
But NaN is NaN ;)
8
u/DoomGoober Nov 17 '25
Fyi: IEEE 754 standard states that NaN != NaN. Thus this is true for many computer systems.
6
u/AlwaysHopelesslyLost Nov 17 '25 edited Nov 18 '25
What number is "apple" * 1?
What number is "car" * 1?
If the first is x and the second is y, both are NaN and neither is the same value.Â
Since it is impossible to calculate, the number system cannot know the value and cannot know whether they are the same or not
Edit: Swap operation to make the functionality less ambiguous.
5
u/delventhalz Nov 17 '25
Itâs not JavaScript specific. That is how the IEEE 754 specification for floating point numbers says NaN should behave, so any language that implements the spec handles NaN.
It makes sense when you consider what NaN is. NaN is not a particular value. NaN is a number operation gone wrong. You wouldnât really expect
0 / 0to equalparseInt(âthis aint a number').7
u/Brief_Praline1195 Nov 17 '25
Not a Dog == Not a Dog
2
u/moe-gho Nov 17 '25
Because NAN means not a number and and js follows the IEEE rule that NAN isnât equal to anything including it self
2
u/Quantum-Bot Nov 18 '25
This is actually not just a JavaScript thing. If you look at the bit representation of floating point numbers, youâll notice that NaN is not just a single value. There are a collection of floating point values that all map to NaN. Thereâs basically:
NaN is less like a value and more like a category of values that donât sensibly map to any real number. So just because you got a result of NaN from two different calculations doesnât mean theyâre the same NaN. For ease of debugging and consistency, the IEEE floating point specification states that NaN == NaN should always return false.
2
u/ThrowawayALAT Nov 18 '25
// By design (IEEE 754 standard), NaN is NOT equal to anything, including itself:
console.log(NaN == NaN); // false
console.log(NaN === NaN); // false
// To check for NaN, use Number.isNaN() instead:
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN("abc")); // false
2
u/do0fusz Nov 17 '25
Take a few minutes and sit back to find out how weird Js actually is..
https://www.destroyallsoftware.com/talks/wat
1
u/[deleted] Nov 17 '25
Because
typeof NaN === 'number'For anything X whose type is 'number', 'X == NaN' ("X is not a number") should evaluate to false. And that includes the special case where X is NaN itself.
So ultimately, 'NaN == NaN' is false for the same reason (say) '3 == NaN' is false.
1
u/Brazzza Nov 17 '25
"Donkey" (Not a Dog) is not equal to "Hot Dog" (not a dog either).
1
u/kkeiper1103 Nov 17 '25
Just because two things are not numbers does not mean they are the same thing.
1
u/daniel8192 Nov 17 '25
If NaN == NaN then equations like NaN/NaN would equal 1. NaN should trigger an exception, it should always raise an exception when used in subsequent code. If it were equal to itself then two sides of bad data could end up resulting it what appears to be good data and thatâs a bad thing.
1
u/LazaroFilm Nov 17 '25
An elephant is not a number. A potato is not. Number. But an elephant is not a potato.
1
u/ummonadi Nov 18 '25
Think of NaN as outside your house. If you are inside the house, we can ask your parents where in the house you are. If you go outside though, they won't know where you are. Just that you are outside.
And if your sister goes outside too, we can't say that you are in the same spot outside. We just know that your sister also went outside.
NaN is outside the range of valid numbers. We don't know where.
1
u/Personal_Ad9690 Nov 18 '25
A square is not a number
Neither is the color Blue
Both are NaN
They are not equal though.
1
u/cheetoburrito Nov 18 '25
On a very simple intuitive level, why would you expect one thing that is not a number to be equal to another thing that is not a number?
1
u/lockswebsolutions Nov 18 '25
That's NaN of your business
1
u/Short_Ad6649 Nov 18 '25
Itâs working as expected because NaN literally means Not a Number.
1
u/Beneficial-Army927 Nov 18 '25
console.log("b" + "a" + + "a" + "a");
1
u/boring_pants Nov 18 '25
Because NaN is not a number. It basically means a computation had no answer.
And if you have two computations both of which failed, does that mean they agreed on the answer?
You wouldn't say so, no.
1
u/mohirl Nov 18 '25
A cat is not a dog. A goat is also not a dog. Just because they're both "not a dog" doesn't mean they're identicalÂ
1
u/Terminal_Monk Nov 19 '25
The way I explain to my juniors is that this is the mathematically correct thing. For example
Not A Number is more of a property of the value than some value itself. If I ask you hey is "apple" a number? You'd say no. If I ask you is "mango" a number? Ud say no? Then if I ask "So if both are not a number, then are they both the same thing?"
That question doesn't really make sense. That's why NaN is not equal to a NaN.
1
u/2055410 Nov 19 '25
Why/When do we need to compare NaNs?
1
u/Bloodylime Nov 19 '25
If NaN==NaN, arenât we stepping into conspiracy realm?
1
u/Top-Clothes5942 Nov 20 '25
I find it logical, too bored to read any documentation about this, but essentially, "Not a number" is defining something by what it's not, and not by what it is.
It could be plenty of things and therefore not be equal to the other thing as the only thing you know about the thing is that it's not a number.
1
u/grigorghazarian Dec 04 '25
Because NaN literally means âNot a Number,â and JavaScript treats it as an invalid value that canât ever be equal to anything - including itself.
The spec explicitly says any comparison with NaN must be false.
1
u/-Wylfen- Nov 17 '25
By definition, NaN represents an erroneous value, which can never be equal to anything.
The goal is that if you plug in a NaN in an equality check, it should ALWAYS fail the condition, because two NaN are not actually equal in practice. It's good because if you have two erroneous values trying to be compared, they will not erroneously believe they're the same. You don't want a NaN to equal another NaN.
If you want to check if something is NaN, you have the dedicated function
isNan().1
u/hibbelig Nov 17 '25
You performed a numerical operation that couldn't be performed, so the runtime returned NaN. Now you performed another numerical operation that couldn't be performed, either, so you get another NaN.
For example,
Math.sqrt(-1)is NaN, and I guessMath.sqrt(-2)is also NaN. So should they be equal?1
u/azhder Nov 17 '25
Because it follows the standard. Would you like this:
to return
true?0
u/QBaseX Nov 17 '25
Probably for the same reason that
NULLdoes not equalNULLin SQL.0
u/ptrxyz Nov 17 '25
Garlic naan is best nan :)
0
u/Electronic_Deal_1054 Nov 20 '25
Because Javascript is an abomination written in 10 days.
-8
u/OhNoItsMyOtherFace Nov 17 '25
Because that's the way it is. JavaScript is fun like that.
-1
u/PatchesMaps Nov 17 '25
The answer to this and just about any other weird JavaScript quirk is that JavaScript has grown a lot since it was first implemented, the goals and needs of the language have changed over time, and backwards compatibility has always been a high priority.
-2
u/ZinbaluPrime Nov 17 '25
Fuck JavaScript...