I personally didn't understand how do JS act with number, but after I learnt about it.
I'll explain each of them and give an example for each of them.
Number: The main Number data type.
null: intentional absence of any object value.
NaN: Not a Number.
whether it's integer like:
or float like:
It is number, if there was a decimal number it'll add it, if not it'll sow up as normal integer.
console.log(5/2); // Outputs: 2.5 console.log(4/2); /// Outputs: 2
We usually use
Number()to turn the variable to number.
Null basically is none. If you made undefended variable it'll equal
And it doesn't mean that it's an empty string or 0.
let nothing; // This variable is undefined. console.log(null == nothing); // Outputs: true console.log(null == 0); // Outputs: false console.log(null == ""); // Outputs: false
That happens because
null only equals the undefined variable.
undefined are equal but not identical.
NaN stands for Not a Number, and it appears if you you have a filled string and converted it to number.
let thing = "Something"; console.log(Number(thing)); // Outputs: NaN
But if you compared
NaN with other
NaN it'll return
let thing = "Something"; console.log(Number(thing)); // NaN console.log(Number(thing) == NaN); // false
How to fix this?
isNaN() method instead.
let something = "Value";
console.log(isNaN(something)); // True
console.log(isNaN(Number(something))); // True