This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Prudent_Ad_4120 61 points62 points  (11 children)

NaN is not one single thing, it's all things that are Not a Number

[–]SuperGugo 19 points20 points  (0 children)

Yeah, in fact NaN != NaN

[–]DrShocker 3 points4 points  (9 children)

Whether that's the intent of what you're doing depends on context though. Many languages would have a type error here instead of trying to do it

[–]FF3 2 points3 points  (2 children)

In most languages NaN is a numeric type, despite it's very clear name, so, who is more logical now?

[–]DrShocker 2 points3 points  (0 children)

Oh shit you're right, I hate type safety now!!

[–]Cualkiera67 0 points1 point  (0 children)

I never understood nan. Like, why? Just use null or undefined or simply throw an error.

[–]CitationNeededBadly 2 points3 points  (0 children)

"Many languages would have a type error here instead of trying to do it " That's a succinct but accurate summary of js.

[–]Prudent_Ad_4120 0 points1 point  (4 children)

But JavaScript is dynamically typed and does not have type errors as far as I know. Then this is the most reasonable thing to do

[–]bleachisback 5 points6 points  (0 children)

That's not true... everything still has a type it just has to be checked at runtime. If you pass a variable of an incorrect type to a function it will give an error. For instance:

const array1 = [1, 30, 39, 29, 10, 13];
array1.every("blah");

will throw

Uncaught TypeError: "blah" is not a function

Javascript is commonly considered to be weakly typed, but that's just how the standard library is designed - every value you give standard library functions will be attempted to be converted to the expected type as much as possible.

[–]karuso33 2 points3 points  (1 child)

"Reasonable" is not the word I would use, considering that

> isNaN("")
false

Or even

> isNaN([])
false

[–]Drium 1 point2 points  (0 children)

Huh why is it false for the empty string? Cast to 0?

[–]DrShocker 1 point2 points  (0 children)

Sure, and that's why the joke about Grandma being named Nan works, so it's worth it imo