Recently, i fell into the trap of NaN and isNaN and it turned me pulling my hair for quite a few good minutes. Thats why i decided to make a note of it here for a recitation and as a note of reference for myself for future.
var x = parseInt("Hello"); //this would label your variable x as NaN.
Now suppose you wanted to check whether x is NaN or not. Having not looked at the isNaN and/or NaN earlier carefully (like me) you may simply fell into this trap and may try to compare x with NaN like this:
if(x==NaN) - incorrect if(x=="NaN") - incorrect
Always use isNaN to compare NaN value.
if(isNaN(x)) - correct, works!
Hello Friends, I’m Arvind Kumar, founder of Inimist Technologies, Inimist Academy, The Fundabook and many more. I am a Full Stack Web Developer and Programmer by heart, and a Hindi/English blogger by chance. I build Web and Mobile Application around React, VueJS, NodeJS and PHP frameworks.
Have a project in mind? Get in touch