Skip to content

OutputNot attempted

Lexical, not dynamic, scope

const name = 'module';
function outer() {
const name = 'outer';
return function inner() {
return name;
};
}
function caller() {
const name = 'caller';
return outer()();
}
console.log(caller(), name);

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 function sees variables from where it was WRITTEN (outer), not from where it was CALLED (caller). JS has lexical scope.