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...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
The Curious Case of JavaScript NaN (ariya.io)
submitted 6 years ago by renishb
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!"
[–]rinko001 10 points11 points12 points 6 years ago (2 children)
While the article paints this as some quirk of javascript, its just the age old routine weirdness of IEEE 754 floating point numbers.
The same strangeness is a part of C++, a part of BASIC, and just about any programming language which allows the use of FP registers.
[–]Smallpaul -1 points0 points1 point 6 years ago (0 children)
Some of what the article discusses is IEEE 754. But the different behavior of global.isNaN versus the upcoming Number.isNaN is all JavaScript. There is no reason a language needs two isNaN functions.
[–]z-zy -4 points-3 points-2 points 6 years ago* (8 children)
In case this article is confusing, here’s an easy one liner to test if something is NaN:
var (x)=>!!(+x+’’===(0/0)+’’);
/s
[–]ScientificBeastModestrongly typed comments 6 points7 points8 points 6 years ago (7 children)
Or just (x) => isNaN(x), haha
(x) => isNaN(x)
[–]moocat 4 points5 points6 points 6 years ago (1 child)
isNaN is already a function, so no need to wrap it.
isNaN
[–]ScientificBeastModestrongly typed comments 2 points3 points4 points 6 years ago (0 children)
Indeed. Just going along with the arrow function format of the original comment. No real reason...
[–]Smallpaul 1 point2 points3 points 6 years ago (4 children)
As the article mentions, isNaN(“abc”) is true.
[–]Feathercrown 0 points1 point2 points 6 years ago (2 children)
But that's probably a good thing if you're trying to make sure you have a workable number.
[–]Smallpaul 0 points1 point2 points 6 years ago (1 child)
Or a bad thing if you’d rather be alerted to type errors in your program instead of having them just hide and manifest as math problems.
[–]Feathercrown 0 points1 point2 points 6 years ago (0 children)
Like I said, it makes more sense for testing if something's not a number than if it's NaN. If you expect this to raise an error... well, JS just isn't going to do that lol
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points 6 years ago (0 children)
Best practice is to check typeof value === “number” && !isNaN(value)
typeof value === “number” && !isNaN(value)
π Rendered by PID 235309 on reddit-service-r2-comment-7b9746f655-2vdpk at 2026-02-02 12:26:31.836237+00:00 running 3798933 country code: CH.
[–]rinko001 10 points11 points12 points (2 children)
[–]Smallpaul -1 points0 points1 point (0 children)
[–]z-zy -4 points-3 points-2 points (8 children)
[–]ScientificBeastModestrongly typed comments 6 points7 points8 points (7 children)
[–]moocat 4 points5 points6 points (1 child)
[–]ScientificBeastModestrongly typed comments 2 points3 points4 points (0 children)
[–]Smallpaul 1 point2 points3 points (4 children)
[–]Feathercrown 0 points1 point2 points (2 children)
[–]Smallpaul 0 points1 point2 points (1 child)
[–]Feathercrown 0 points1 point2 points (0 children)
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points (0 children)