Sub topic 1
What Scope Means in JavaScript
Every time your code uses a name such as total, user, or render, JavaScript has to work out which variable or function that name refers to. Scope is the set of rules that answers this question. A variable's scope is the region of code where you can refer to it by name.
This chapter introduces the main idea behind those rules, lexical scope. Later chapters cover each kind of scope and the lookup process in detail.
Prerequisites
This chapter assumes you can already:
- Read and write basic JavaScript syntax
- Declare variables with
var,let, andconst - Write and call functions
- Use
ifstatements andforloops - Run code in a browser console or with Node.js
You don't need any prior knowledge of how scope works.
Scope Is About Visibility
A name declared in one part of a program is not visible everywhere. Here, secret exists only inside reveal:
function reveal() {
const secret = 42;
return secret;
}
console.log(reveal()); // 42
console.log(typeof secret); // "undefined" (not visible out here)Scope does two jobs. It keeps names from colliding across unrelated parts of a program. It also limits which code can read or change a value. Both make programs easier to reason about.
Lexical Scope: Where Code Is Written
JavaScript uses lexical scope, also called static scope. "Lexical" means the scope of a name is decided by where the code sits in the source text. Where or how a function is later called makes no difference.
The example below shows this. show is called from inside caller, which has its own name. The result still comes from where show was written:
const name = "global";
function show() {
return name;
}
function caller() {
const name = "caller";
return show();
}
console.log(caller()); // "global"show was written at the top level, so it can see the top-level name. It cannot see caller's local name, because show is not written inside caller. A language with dynamic scope would print "caller". JavaScript does not work that way.
Scopes Nest
Functions and blocks can be written inside one another, so scopes form a nested structure. Code in an inner scope can see names from every scope that encloses it. Code in an outer scope cannot see names declared inside an inner one.
JavaScript has four kinds of scope, and each one gets its own chapter:
| Scope | Created by | Example declarations |
|---|---|---|
| Global | The top level of a script | `var`, `let`, `const` at top level |
| Function | Each function body | Parameters, any declaration inside |
| Block | Any `{ }` block | `let`, `const`, `class` |
| Module | Each ES module file | Top-level declarations in a module |
A Common Misconception
Many developers assume a function can use variables from the place where it is called. This assumption leads to ReferenceError bugs and to code that reads the wrong value without any error. Remember that a function brings its written surroundings with it wherever it goes. This idea is the foundation for closures, covered later in the tutorial.
Exercise
What does this code print, and why? const color = "blue"; function paint() { return color; } function room() { const color = "red"; return paint(); } console.log(room());
Show answer
It prints "blue". JavaScript uses lexical scope, so paint resolves color based on where paint is written (the top level), not where it is called from. The "red" declared inside room is not visible to paint.
Exercise
A variable is declared with const inside a function body. Can code at the top level of the script read it? Explain using the idea of nested scopes.
Show answer
No. Inner scopes can see names in the scopes that enclose them, but outer scopes cannot see names declared inside inner ones. The top level encloses the function, so it cannot see the function's local variables.