Lesson: Understanding Uninitialized Variables

Lesson: Understanding Uninitialized Variables
0

#1

hey

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;
console.log(typeof(eee));
the console printed number


#2

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?


#3

Did you actually type that?


#4

NaN explained on MDN


#5

@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.