3

This expression is giving me the undefined output in this console. Please help to understand why it is printing undefined?

var a = 3, b = a = typeof b; // undefined

2 Answers2

2

What really happens here is:

var a = 3;    // so a gets a value.
var b;        // var b exists but is undefined.
a = typeof b; // a receives type of b, which is undefined at this time
b = a;        // b gets the value from a
Kevin Verstraete
  • 1,363
  • 2
  • 13
  • 13
1

An assignment operator assigns a value to its left operand based on the value of its right operand. That is, a = b assigns the value of b to a.

Thus, in our case

b = a = typeof b;

a gets the value undefined and then b.

var a=3, b = a = typeof b;
undefined
a
"undefined"
b
"undefined"