+ 9

# NaN in JavaScript confused me

var num = 8 * undefined console.log(num)// output NaN console.log(typeof(num))// output number // NaN means not number // so how can be the typeof NaN number?

12 Answers

+ 11

NaN is Not A Number.
if you want to do any operation that result will not a number this time you get out put NaN.
Look, if you want to do arithmetic operation with number and string it not possible. the outupt is NaN.
To better observation, try this
"100" - "10" //90
above this is string but do arithmetic operation.
Now is it clear?!!

+ 8

Welcome bro.đ

+ 5

NaN is typeof number because it is the result of a calculation that couldnt be calculated.
I think NaN is defined in the number object and therefore type of NaN

+ 5

thank you now i got it

+ 4

plz tell what does mean NaN

+ 4

NaN is the result of mixing non-numbers is math equations.
If the variable is still of the type Number you should use isNaN() to test for your case.

+ 4

The NaN property represents "Not-a-Number" value. This property indicates that a value is not a legal number. The NaN property is the same as the Number. Nan property. Tip: Use the isNaN() global function to check if a value is a NaN value.

+ 3

Not a Number

+ 3

Nan is Not a Number, if u calculate a number and string, the output is NaN

+ 3

thank you Maher F
it helpfull info

+ 3

thank you for answer Dhara Paghadar

+ 1

NaN is a property of the global object that represents "Not-a-Number" value. This property indicates that a value is not a legal number. it is rather rare to use NaN in a program directly like so:
var notANumber = NaN;
I think NaN is designed to check for valid user inputs or function parameters.
for instance:
if(isNaN(usrInput)) {
doSomthingWith(usrInput);
}
or
function doSomthingWith(x) {
x = (isNaN(x)) ? 0 : x;//set x to 0 if the value of x is NaN
}