Lesson: Understanding Uninitialized Variables


in this lesson i read that if you do a mathmatical operation on undefined variable you will get NaN.

i did it and i got a number, why?

also, what is NaN?

this is what i did:

var str;
var eee = 5 + str;
the console printed number

Not a Number is a Number just Not a Number. Also, NaN === NaN is false; No NaN equals NaN. So typeof(e) is number, but just e is NaN cause you’re adding 5 to an undefined variable. Make sense?

Did you actually type that?

NaN explained on MDN

@tomer-n you declared str you just didn’t define it, so when you add str to 5 you get NaN because the math doesn’t work. Just like dividing 0 by 0 will give you NaN, even though it’s of a “number” type.