javascript - Why Initialization of several variables leading into Scope Leakage? -


i referring docs of javascript var hoisting , there in section found initialization of several variables example given below.

var x = 0;  function f(){   var x = y = 1;  } f();  console.log(x, y); // outputs 0, 1 // x global 1 expected // y leaked outside of function, though!  

where suppose exception uncaught reference error: y not defined. not happening due leaked scope , displaying 0,1.

can know why happening in detail , made happen. performance related issues ?

you're not declaring y.

var x = y = 1;  

is equivalent to

y = 1; var x = y; // actually, right part precisely result of assignement 

an undeclared variable global variable (unless you're in strict mode, it's error).

the example you're referring different, there comma, is part of multiple declaration syntax.

you fix code in

var y=1, x=y; 

Comments