+ 5

In Javascript NaN is not equal to NaN. Why?

12th May 2017, 12:00 PM
Jacob Makau
Jacob Makau - avatar
5 Antworten
+ 8
Yep.... To check if Number is NaN or not use isNaN(0)
12th May 2017, 12:42 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 8
😯 Don't make me feel like I'm stupid again Btw thanks for sharing
12th May 2017, 1:02 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 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.
12th May 2017, 12:26 PM
Benneth Yankey
Benneth Yankey - avatar
+ 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.
12th May 2017, 1:01 PM
Benneth Yankey
Benneth Yankey - avatar
+ 3
@Very Hard you are welcome
12th May 2017, 1:04 PM
Benneth Yankey
Benneth Yankey - avatar