Lesson 2.5Lesson 11 of 24
Scope and execution flow
Given a block of code, analyze the variable scope and the execution flow.
Key points
- 1var is function-scoped and hoisted as undefined. let and const are block-scoped and throw if read before their line.
- 2A name is looked up from the inner scope outward. An inner declaration shadows an outer one with the same name.
- 3Scope depends on where a function is written, not on where it is called.
- 4A closure captures variables, not values: it sees later changes. A var loop shares one variable; let gives each iteration its own.
What the exam guide checks here
Objective wording from the exam guide (English).
- 2.5.K1Global, function and block scope; the scope chain; shadowing
- 2.5.K2Closures that capture variables, including loop variables
- 2.5.S1Trace a block of code and state what it logs or returns
Lock it in
Answer scenario questions on this topic. Every option comes with an explanation.
Practise 2.5 · 12 questions