1 hour later…
1:35 AM
3 hours later…
4:12 AM
@RyanM I know, I know. I do have an explanation that tends to work, though. So, you have some value that you need to convert to a numeric and only numeric. So, you have a function that does
any_value -> number
conversion. However, you eventually get a value that cannot be converted, like "apple"
. The function doesn't throw errors, it still must produce a number. So, it can only return you something from the numeric realm signifying the result is not a number.
And then, you get
NaN
. Maybe the input was "apples"
one time, which returns NaN
. And another might have been "orange"
which is also NaN
. It's also why NaN != NaN
- you can't compare the numeric representations of two non-numerics. After all "apple" != "orange"
you can't suddenly claim they are equal after the conversion.
6 hours later…
10:17 AM
Also, in researching whether JavaScript has a concept of "numeric", I found this trash fire of a question, which asks in the title, how to "Check that variable is a number", but then defines "a number" as "either an integer or a string digit" in the body.
Naturally, the top answer is ...just totally wrong? It correctly answers neither question, suggesting
isNaN
(which, presumably, one would need to invert), which would find such things as booleans, the string "Infinity", the empty string, and a space to be numeric. 10:33 AM
Then there's the apparent canonical, Validate decimal numbers in JavaScript - IsNumeric(), which has a beautifully tested answer with nearly 3k net score ... which fails one of the tests in the question due to a disagreement over whether hexadecimal strings count are numeric. They even wrote a test validating that it produces behavior contrary to the question. Naturally, it's accepted despite this.
5 hours later…
3:36 PM
@RyanM Well, yes - has to be inverted. But the answer is just mentioning which function to use and that it returns
true
if it's not a number. It does, in fact, give you the exact same result as the question asks. Which...is also a problem. isNaN
is very badly named, because the real thing it checks is not "is this value not a number" or "is this value the numeric value NaN
". The real check is "if I convert this value to a number, is the result NaN
or not". So, isNaN("42")
-> false
because converting the string "42"
to a number produces a valid number. But isNaN("apple")
-> true
It's better JS style to use
typeof(something) == 'number'
. With ==
rather than ===
. Reserve ===
for when you really mean it. — Adam Chalcraft Feb 22, 2020 at 23:35
To be clear, it's bogus. There is no reason to demand more differences in equality checks.
===
is literally always correct. There is a single case where a loose (==
) equals might be preferable but it's more down to style. There is zero downside to always use ===
. There are many potential pitfalls if you use ==
and ===
inconsistently. « first day (3220 days earlier) ← previous day next day → last day (38 days later) »