Hint
var ignores block scope and shares with the function. let is block-scoped — the inner y is a completely separate variable.
function test() {
var x = 1;
let y = 2;
if (true) {
var x = 3; // same var x — function scoped
let y = 4; // new y — block scoped
console.log(x, y);
}
console.log(x, y);
}
test();3 4 3 2
Explanation: Inside the block: var x=3 overwrites the outer x, let y=4 is new. After the block: x is still 3 (var leaked out), but y is 2 again (block let is gone).
Key Insight: var ignores block scope and shares with the function. let is block-scoped — the inner y is a completely separate variable.