Do you know what will be the output of this code?
JavaScript
var y = 1;
if (true) {
var y = 2;
}
console.log(y);
Show answer and explanation
The answer is 2.
var is function-scoped, not block-scoped. Both declarations refer to the same variable, so the assignment inside the if changes it.