A function is defined in one place and called from somewhere else. When it reads a variable, does it look where it was written or where it was called?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
show prints "outer": it looks up label where show was written, not where it was called.
const label = "outer";
function show() {
console.log(label);
}
function run() {
const label = "inside run";
show();
}
run();
03Objective and prerequisites
Learning objective: Describe lexical scope and the scope chain, and predict variable lookup by reading where functions are defined, not where they are called.
Code reviewers and editors resolve every name by reading the nesting of functions and blocks. Because JavaScript scope is lexical, "go to definition" in an editor always finds the same variable the program will use.
6. A helper needs the current user's name. Which is clearer?
Read a global currentUser
Accept the name as a parameter
Read it from the caller's local variable
Use var so it leaks
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Pass the value in: Prints Hello, Grace; greet works alone
Trace the lookup: Prints global; Calls a()
Configuration reader: Prints both; Unknown key throws; Readers are independent
ex-047-01: output matches the documented expected output
ex-047-02: output matches the documented expected output
ex-047-03: output matches the documented expected output
ex-047-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-10-scope-and-javascript-behavior/lesson-047-lexical-environments/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Scope is lexical: decided by where code is written.
Lookup walks outward along the scope chain.
Each call creates a new environment linked to its definition's scope.
Pass values in as parameters rather than relying on the caller.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationlexical scope and the scope chain
Context: I am learning lexical scope and the scope chain in JavaScript.
Task: Explain lexical scope and the scope chain with nested functions, and how the engine looks up a name.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionlexical scope and the scope chain drill
Context: I have practised lexical scope and the scope chain.
Task: Give me six snippets where a function is defined in one place and called in another, and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debugginglexical scope and the scope chain bug
Context: My function cannot see a variable that exists where I call it.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationlexical scope and the scope chain practice
Context: I finished the lexical scope and the scope chain exercises.
Task: Create three exercises that trace scope chain lookups.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewlexical scope and the scope chain review
Context: Here is my code that uses lexical scope and the scope chain.
Task: Review my code for functions that depend on hidden outer variables instead of parameters.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.