Skip to content

OutputNot attempted

var inside an if that never runs

var v = 1;
function g() {
console.log(v);
if (false) {
var v = 2;
}
console.log(v);
}
g();

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

var ignores blocks: var v inside the if is hoisted to the top of g and shadows the outer v, but the assignment never runs → undefined, undefined.