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;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments