+ 5
In Javascript NaN is not equal to NaN. Why?
5 Respuestas
+ 8
Yep....
To check if Number is NaN or not use
isNaN(0)
+ 8
😯
Don't make me feel like I'm stupid again
Btw thanks for sharing
+ 6
Bcos' NaN results from nonsensical operations. And how can a result of NaN operation equals NaN operation it doesn't make sense. So NaN == NaN // false
is a good design. And that's how it's built you can't change it.
+ 6
@Very Hard
isNaN() just checks "literally". That's it doesn't just check whether the type or value is actually a NaN.
For instance,
isNaN(1) // false
isNaN(" user") // true
Here "user" is not a number so it returns true, BUT "user" is not an actual "NaN". So you see it just checks literally.
To actually check if a type or value is an actual " NaN". ES6 solves it by using "Object.is(value, NaN)".
For instance:
Object.is(1, NaN) // false
Object.is(" user", NaN). // false
Object.is(NaN, NaN) // true
So from above Object.is() returns true if and only if the compared value is truly NaN.
+ 3
@Very Hard you are welcome