Re:WTFJS – a list of funny and tricky JavaScript examples
by xyclos on Monday July 31, 2017 @09:38AM
(#14891810)
NaN is meant to represent a non-sensical mathematical operation (like divide by 0). One non-sensical mathematical operation is not the same as some other non-sensical mathematical operation (1/0 !== Infinity/Infinity)
[ Parent
| on HN ]
Re:WTFJS – a list of funny and tricky JavaScript examples
by Pigo on Monday July 31, 2017 @09:44AM
(#14891862)
That's a good point, a lot of this seems like it can be explained with some context or just asking why you'd ever want to do that.
Re:WTFJS – a list of funny and tricky JavaScript examples
by gmiller123456 on Monday July 31, 2017 @04:43PM
(#14895731)
>NaN is meant to represent a non-sensical mathematical operation
This is not a very good explanation. NaN is used to represent a nonsensical value. By your explanation (1/0 === 1/0) should be true since they represent the same invalid opperation. The fact that NaN != NaN is not meant to mean anything, it is merely defined that way to prevent a class of bugs from occurring. Anyone not familiar with the definition is correct to be confused by it. I'd think if the spec was designed today, this special case (hack) wouldn't be there and we'd have exceptions in its place.
Re:WTFJS – a list of funny and tricky JavaScript examples
by justinpombrio on Monday July 31, 2017 @11:06AM
(#14892663)
Nor is it the same as the same non-sensical mathematical operator :-)
1/0 !== 1/0
Re:WTFJS – a list of funny and tricky JavaScript examples
by tambourine_man on Monday July 31, 2017 @11:46AM
(#14893019)
That's mad. In JS, 1/0 is Infinity, not Undefined. And Infinity is not equal to itself.
Re:WTFJS – a list of funny and tricky JavaScript examples
by tambourine_man on Monday July 31, 2017 @10:09AM
(#14892106)
That's a good explanation, thanks.