Why does z() execution context not override global x variable?
var x = 10;
function z(){
var x = x = 20;
}
z();
console.log(x); // why is 10 printed? Shouldn’t it be 20.
var a = b = c = 0;
It means b and c are being declared as globals, not locals as intended.
For example -
var y = 10;
function z(){
var x = y = 20; // global y is overridden
}
z();
console.log(y); // value is 20
Going by above logic, x = x = 20 in z() means x is global which overrides the local x variable but still global value of x is 10
xin the function is local to the function, your log use the global, which is 10var x = 10. HereXis a global variable, right ? Now, inside yourzfunction, its aLOCAL Variable. if you remove thevarfrom your function. It will print 20. Because it will be refering to the global one and not the local variable.20. See Ori Drori's answer.var a = b = c = 0;onlyais declared, other variables refer to earlier declared variables or to an outer scope. If you want a single line declaration, you use comma to separate the declarees.