NaN Is Actually A Number In JavaScript

What is NaN in JavaScript

NaN is short for "not a number". Butm wait, NaN is actually a number
var number = Number('sometext');
console.log(number);
// NaN
console.log(typeof(number));
// number

Varify if something is NaN

Sometimes we want to veriru if something is a number then proceed execution of following code. But the following code will not work
if(Number(something) == NaN){...}
if(Number(something) == "NaN"){...}
We need to use this:
if(Number(something){...}
if(!Number(something)){...}
This works because NnN kind of means 'true'
var number = Number('sometext');
console.log(number == false);
// false
console.log(!number == true);
// true
console.log(!!number == false);
// true
if(number){
	console.log('This is not a NaN');
}else{
	console.log('This is a NaN');
};
//This is a NaN

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser