Skip to content

OutputNot attempted

Closure captures the variable, not the value

let x = 1;
function show() {
console.log(x);
}
setTimeout(show);
x = 2;

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.

A closure holds a live reference to the variable binding. By the time the timer runs, x is 2.