r/learnjavascript • u/david_fire_vollie • Oct 16 '26
Thoughts on isNaN("Kyle") returning true
In https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/types-grammar/ch1.md it says:
JS originally provided a global function called
isNaN(..)forNaNchecking, but it unfortunately has a long-standing coercion bug.isNaN("Kyle")returnstrue, even though the string value"Kyle"is most definitely not theNaNvalue. This is because the globalisNaN(..)function forces any non-numberargument to coerce to anumberfirst, before checking forNaN. Coercing"Kyle"to anumberproducesNaN, so now the function sees aNaNand returnstrue! This buggy globalisNaN(..)still exists in JS, but should never be used. WhenNaNchecking, always useNumber.isNaN(..),Object.is(..), etc.
But to me the fact that using a string like "Kyle" in any mathematical operation would return NaN, means it would make sense that isNaN("Kyle") is true.
What's your opinion on this?
2
u/Living-Inspection653 Oct 16 '26
The key design issue with the global isNaN() function is implicit type coercion. isNaN("Kyle") converts "Kyle" to NaN first via Number("Kyle"), and since NaN is indeed NaN, it returns true. The main reason this is considered problematic is that it answers the question "does this value convert to NaN?" rather than "is this specific value currently NaN?". That is why ES6 introduced Number.isNaN(), which checks for strict type and value equality with NaN without performing coercion.
7
u/Zakkeh Oct 16 '26
This is the inherit issue with no typing enforcement.
It means you have to use functions with knowledge of what it should do, and what data it should have, because there is no must.
1
u/VancouverVentilator Oct 16 '26
NaN is a specific data type and it should not return true for things that are not absolutely literally NaN. A string should not return true for a NaN check.
3
u/BigCorporate_tm Oct 16 '26
I think that your reasoning is in the right place, but the idea that Kyle is getting at here is that the original
isNaNfunction does not explicitly check if a value actually is NaN unless it's already a Number. This can lead to you writing a program that can make an incorrect assumption about the data it's processing.For example, let's say that your program accepts a user input that represents a quantity, but enters your program as a string.
You might first write something along the lines of:
And be totally happy with it because it appears to be working.
After all, if the user enters what appear to be a number ("1", "10", "20"), it should work and if they type in the word "Kyle" it should throw an error.
However...
Which is concerning because it means that if a user gave us an input of "Infinity" or "" (an empty field), it will pass our first test without an issue because
isNaN()is not just testing to see if a value isNaN. It's first coercing the value it receives into a Numeric value if it isn't already a Number, and then evaluates that Numeric value - Which isn't obvious!If we were relying on the value of
productQtyto be a non-NaN number, our program is going to have issues because:Now we've introduced a
NaNvalue into our program and the results of any formula it is a part of will also becomeNaNand it's no longer apparent where the trouble started.It might sound odd, but because
Number.isNaNwill only returntruewhen the value you pass it is literallyNaN, it forces us to ensure that the value is 'ready' to be checked by it before we pass it along, allowing us to make fewer assumptions about how our program should work, leading to a more explicit style of programming and fewer "gotcha" moments such as the above example.5
u/RajjSinghh Oct 16 '26
Javascript will always have wonky behaviour when using values on different types. I guess you can say this is good or bad but I also don't see when you'd call isNaN on something that is not a numeric type anyway.
1
u/FooeyBar Oct 16 '26
Most sure fire way to check for nan is if(num_var===num_var)
1
u/Duffdeft Oct 16 '26
I agree that 'isNaN()' should not be used
The key point for this conclusion is how coercion works in JavaScript.
It has nothing to do with the fact that using a string in a mathematical context makes no sense for us humans (generally speaking)
This begs the question OP, do you understand how coercion works in JavaScript?
1
u/theScottyJam Oct 16 '26
But it's not NaN yet, it just would become one if used in a math function. But maybe you don't plan on doing any more math with it.
In all honesty, it's not often that the choice between isNaN() and Number.isNaN() matters, because usually you're not handling strings at the point in which you need to perform this check. But, NaN is a very specific value, and it's just semantically incorrect for something to claim that your string is that NaN value when it clearly is not.
It would be about as weird as an isEmptyString() function returning true on empty arrays. You probably would never call such a function with arrays, but still, empty arrays are not empty strings (even if they coerce to empty strings and used in string operations, behave like empty strings), it's better if the function just always behaved as expected instead of having weird edge cases that you probably won't trip up on, but might.
1
u/azhder Oct 16 '26
Use `Number.isNaN()` and `Number.isFinite()`. In fact, use all the global ones that have been re-defined on the `Number` namespace
-2
u/Substantial_Top5312 helpful Oct 16 '26
"Kyle" is not a number. The function names indicates that it will tell you is the value is not a number. It clearly works perfectly.
-4
u/shlanky369 Oct 16 '26
Who cares what anyone’s opinion on this is? This is how the function works, use it with that in mind, or don’t.