Why does the following code result in logging of b while a is still undefined?
(function(){ var a=b=5; })();
console.log('b:'+b);
console.log('a:'+a);
Because var a=b=5; statement defines only local a variable and in fact is interpreted like
var a = (b=5);
which equals to
b = 5;
var a = 5;
which assigns 5 to a global b variable and defines a local a variable.
The proper way to define 2 local variables without value repetition would be
var b = 5, a = b;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With