Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript (Effective Software Development Series)

Category: Programming
Author: David Herman
This Year Stack Overflow 1
This Month Stack Overflow 1


by anonymous   2017-08-20

I just came across this technique in the book Effective JavaScript that is pretty simple:

Since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself:

var a = NaN;
a !== a; // true 

var b = "foo";
b !== b; // false 

var c = undefined; 
c !== c; // false

var d = {};
d !== d; // false

var e = { valueOf: "foo" }; 
e !== e; // false

Didn't realize this until @allsyed commented, but this is in the ECMA spec: https://tc39.github.io/ecma262/#sec-isnan-number