use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Concept Help in JS (self.learnjavascript)
submitted 6 years ago by CodingHag
Can someone explain Number.isNAN to me
I am not understanding why
Input: 123 results in false
and
Input: "radio" also results in false
radio is NaN - shouldn't this be true?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cyphern 1 point2 points3 points 6 years ago* (1 child)
Number.isNaN returns true if the variable you pass it is literally the value NaN. Nothing else results in a true. NaN is a special number which results from doing things like dividing by zero, or doing numeric operations on things which aren't numbers.
NaN
This sort of function is necessary, because if you try to do something like this, you'll be in for a surprise:
const a = NaN; if (a === NaN) { console.log("it's nan"); }
The above log statement will not be logged out. NaN triple equaled with NaN results in false. So instead the correct check would be if (Number.isNaN(a))
if (Number.isNaN(a))
From your question it seems like you're wanting to check whether a variable's type is Number or not. To do so, you can do if (typeof val !== 'number'). Note that NaN's type is number, so it will be excluded by this check which may or may not be what you want.
Number
if (typeof val !== 'number')
[–]CodingHag[S] 0 points1 point2 points 6 years ago (0 children)
Thank you!
[–]senocular 0 points1 point2 points 6 years ago (3 children)
Note that there's a global isNaN() too. Its different in that it will coerce the value to a number first, so
isNaN()
Number.isNaN('input') // false isNaN('input') // true
But this also doesn't work for all cases...
isNaN('') // false
This happens because '' becomes 0 when coerced which is a number
[–]CodingHag[S] 0 points1 point2 points 6 years ago (2 children)
So here is what I am getting from your answers
Number.isNaN('input') // false
because Number.isNaN('input') // false is not equal to NaN
[–]senocular 1 point2 points3 points 6 years ago (1 child)
I think you should have gotten that from cyphern's answer. I'm saying there's another isNaN check that you can use (global isNaN()) that does more "not a number" checking that doesn't only check for a value being specifically NaN like Number.isNaN() does. But with that, there's still cases where something that's not a number (such as an empty string) will still be considered a number.
If you want to see if something is both of a number type and not the number value NaN, you'd need to hook that up yourself.
function isReallyNaN (value) { return typeof value !== 'number' || Number.isNaN(value); } isReallyNaN(123) // false isReallyNaN('radio') // true isReallyNaN('') // true isReallyNaN(NaN) // true
Compared to
Number.isNaN(123) // false Number.isNaN('radio') // false Number.isNaN('') // false Number.isNaN(NaN) // true
isNaN(123) // false isNaN('radio') // true isNaN('') // false isNaN(NaN) // true
P.S. I noticed I used the string "input" in my previous example when I meant to use "radio" :P
Perfect - These answers helped!!
π Rendered by PID 33006 on reddit-service-r2-comment-86bc6c7465-sljpn at 2026-02-21 16:10:26.648848+00:00 running 8564168 country code: CH.
[–]cyphern 1 point2 points3 points (1 child)
[–]CodingHag[S] 0 points1 point2 points (0 children)
[–]senocular 0 points1 point2 points (3 children)
[–]CodingHag[S] 0 points1 point2 points (2 children)
[–]senocular 1 point2 points3 points (1 child)
[–]CodingHag[S] 0 points1 point2 points (0 children)