NaN Bug and Solution in Javascript
Javascript is very beautiful and growing language. It has some in-built bugs.
NaN — Not a Number
Everyone well known NaN is Not a Number. But its not accurate. We can say NaN is Bad Number or failed Number than the Not a Number.
var x = 10/ 'foo'; //NaNtypeof x // 'number'
x is not a number but the typeof x is return number. But x has the NaN. Its too misleading behavior. And we cant validate directly
x == NaN; //falseSolution
Get parthiban’s stories in your inbox
Join Medium for free to get updates from this writer.
Javascript has one built in method called isNaN. So we can use that method to validate this variable.
isNaN(x) // trueIs problem solved. Nope.
var y= 'foo';isNaN(y); //true
variable y is not a number, but value is not NaN. How can handle this kind of situation?
We have the option in ES6
Number.isNaN(y);or we can write the polyfill
if(!Number.isNaN(y)) {
Number.isNaN = function(n) {
return n !== n;
}
}





