When it's able to pick up w from the outer scope, why is it not able to pick up z?
var w = 1, z = 2;
function foo( x = w + 1, y = x + 1, z = z + 1 ) {
console.log( x, y, z );
}
foo();
it's able to pick up
wfrom the outer scope
Yes, because you don't have a variable w inside your function.
why is it not able to pick up
z?
Because your parameter declares a local variable with the name z, and that shadows the global one. However, the local one is not yet initialised with a value inside the default expression, and throws a ReferenceError on accessing it. It's like the temporal dead zone for let/const,
let z = z + 1;
would throw as well. You should rename your variable to something else to make it work.
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