function x(){
function y(){
console.log(z);
}
var z = 20;
y();
}
var z = 25;
x()
explain the reason for your answer in the comments
I could have analyzed it like j did...or I could just Copy -> Open Dev Tools -> Paste.
It's 20.
it is 20.
this is how I understand it. Please correct me if I am wrong.
1. function x(){ 2. function y(){ 3. console.log(z); } 4. var z = 20; 5. y(); } 6. var z = 25; 7. x()If I rewrite the code like
function x(){ function y(){ console.log(z); } y(); } var z = 25; x() VM220:3 25Now output will be 25, because it will find z inside window object, which value is z
This concept known as "Scope Chaining"