Hoisting and scope: predict each line
Inside show, the local var x is hoisted to the top of the function and shadows the global one, so the first line reads the local x while it is still undefined. inner reads x from where it was written (inside outer), not from the global scope.
Most "what does this print?" interview questions test four rules: how hoisting sets up variables before code runs, how lexical scope decides which variable a name means, how closures keep variables alive, and how a call decides this.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Predict the output, then explain: var a = 1; function f() { console.log(a); var a = 2; console.log(a); } f(); console.log(a);
Predict the output: const o = { n: 1, get() { return this.n; } }; const g = o.get; console.log(o.get(), g.call({ n: 5 }), (o.get)(), (0, o.get)()); (classic sloppy script, and assume window.n is undefined).
Write once(fn) that returns a function which runs fn only the first time and returns the first result on every later call, keeping this and arguments.
Explain it without notes
Explain hoisting for var, let/const and function declarations, including the temporal dead zone.
Why does for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)) print 3, 3, 3, and how do you fix it?
What is a closure? Give a real use.
List the rules that decide this, in order of priority.
Why do arrow functions behave differently with this, and when is that a problem?
Preview
Console
Nothing logged yet.Expected console output
undefined
ReferenceError: Cannot access 'b' before initialization
function declarations are hoisted whole
TypeError: notYet is not a function
undefined
outer
var ignores blocks undefined