Environment Records: how scope is specified
Read the ECMAScript model of bindings, the global object, this, module imports, private names, and outward lookup with small runnable examples.
- 01Explain an early readSay why reading a
letname before its line throws aReferenceError, even when an outer name has the same spelling. - 02Tell the kinds of record apartSay which kind of record holds a name: a plain list, an object's properties, the global record, a function call, a module, or a class's private names.
- 03Follow the outer linkTrace how JavaScript looks for a name one record at a time, and find the binding that a closure or an import really reads.
Names need a home
In JavaScript, two variables can have the same name but hold different values. So when your code reads a name, JavaScript has to decide which variable you mean. The specification (the official rulebook for JavaScript, also called the spec) explains this with Environment Records.
An Environment Record is a list of names. Each name in the list has a binding. A binding holds two things:
- the name's value, and
- whether that value is ready to read yet.
const user = "Asha";
{
const user = "Sam";
console.log(user);
}
console.log(user);Line 1 makes a user that holds Asha. Line 3 makes a second user, inside the curly braces. Line 4 is inside the braces, so it prints Sam. Line 6 is outside them, so it prints Asha. The inner user only counts while the code is inside the braces.
An Environment Record is the spec's way to connect a name to its binding. It is not a JavaScript object, and your code can't look inside it. The spec also says engines may store it any way they like.
You have met this idea before, in scope and lexical environments, closures, and the temporal dead zone. This lesson doesn't teach those again. It shows the exact rules the spec uses. The Reference type lesson showed what JavaScript gets back after it finds a name. This lesson shows the record where it finds the name.
A plain list of names
A Declarative Environment Record is the simplest kind. It is a plain list of names and their bindings. Names get into it through declarations: lines such as let price = 3 or const order = "tea". A block of code inside curly braces gets one of these lists for its own let and const names. The names are not properties of any JavaScript object.
Two other kinds you'll meet later, function records and module records, are declarative records with a few extra parts.
let price = 3;
price = 4;
const order = "tea";
console.log(price, order);Line 1 makes price and gives it 3. A let binding is mutable: you can give it a new value later. Line 2 does that and changes it to 4. Line 3 makes order and gives it tea. A const binding is immutable: you can't give it a new value later. Line 4 prints 4 tea.
Immutable only means the name can't get a new value. If a const holds an object, you can still change that object's properties.
Your school keeps one attendance register for each class. A name in one class's register doesn't replace the same name in another class's register. The teacher always looks in her own class's register first.
- In real life: A line in the register has a student's name
- In JavaScript: A record has a name with its binding
- In real life: The teacher fills in the line on the first day
- In JavaScript: The declaration gives the first value
- In real life: Another class has a student with the same name
- In JavaScript: Another record can have the same name
Declarative records also hold a few other names. One is the error name in catch (error). The main point is how they store names: straight in the list, not as properties of an object. This matters later, for the global record.
A name exists before its value
For let and const, the spec does two separate jobs:
- Create the binding. The name now exists, but it has no value yet.
- Initialize the binding, which means give it its first value. This happens when the declaration line runs.
Between the two jobs, the binding is uninitialized: it exists but has no value you can read. This gap is called the temporal dead zone (TDZ). If you read the name during the TDZ, JavaScript throws a ReferenceError.
{
try { console.log(price); }
catch (error) { console.log(error.name); }
let price = 3;
console.log(price);
}Line 2 tries to print price before line 4 has run. The read throws, and the catch prints the error's name, ReferenceError. Line 4 gives the binding its first value, 3. Line 5 then prints 3.
Line 2 doesn't look for another price further out. JavaScript already found the block's own price. It just wasn't ready yet.
price exists from the start, but it is empty until line 4. Reading it on line 2 throws; reading it on line 5 gives 3.The box below is a shortened summary of the spec's binding steps. It is not JavaScript, so you can't run it. Each line starts with the name of one spec step, then says what that step does. In CreateMutableBinding, "mutable" means the value can change later, as with let.
CreateMutableBinding("price", false): create an uninitialized mutable binding.
CreateImmutableBinding("user", true): create an uninitialized immutable binding.
InitializeBinding("price", 3): set its value and mark it initialized.
GetBindingValue("price", true): if uninitialized, throw ReferenceError; otherwise return its value.In plain words, line by line:
- For
let price: add the namepriceto the list. It can change later. It has no value yet. - For
const user: add the nameuserto the list too. It can't change later. It has no value yet either. - When the declaration line runs: put the first value,
3, inprice, and mark it as ready. - When code reads
price: if it isn't ready, throw aReferenceError. If it is ready, give back its value.
So the name exists from the start of the block, and the read checks whether it is ready.
The table lists the same four spec steps. Read each row from left to right: the step, what it does, and when it happens in your code.
| Spec step | What it does | When it happens in your code |
|---|---|---|
| CreateMutableBinding | Adds a name that can change later. The name has no value yet. | At the start of the block, for let price. |
| CreateImmutableBinding | Adds a name that can't change later. The name has no value yet. | At the start of the block, for const user. |
| InitializeBinding | Gives the name its first value and marks it as ready. | When the let or const line runs. |
| GetBindingValue | Gives back the value. If the name isn't ready yet, it throws a ReferenceError. | Each time your code reads the name. A read before the declaration line throws. |
One more step, SetMutableBinding, handles later changes like price = 4. Don't mix it up with the first value. The first value fills an empty name. A later change replaces a value that is already there, and only works for a mutable name. You can read the full declarative record steps in the spec, next to the example you can run.
Names that come from an object
An Object Environment Record doesn't keep its own list of names. Instead, it uses the properties of one object as its names. The old with statement shows this most clearly. with (order) lets you write price to mean order.price.
with is not allowed in strict mode, and you shouldn't use it in new code. It appears here only because it makes this kind of record easy to see.
const order = { price: 3 };
with (order) {
console.log(price);
}
console.log(order.price);Line 1 makes an order object with a price property. Line 2 starts a with block for that object. Line 3 prints 3: the name price was found as a property of order. Line 5 reads the property the normal way, with a dot, and also prints 3.
The order object is real. The Object Environment Record is only the spec's way to explain how the name price was found.
Because the names come from an object, a few extra rules apply:
- Properties the object gets from its prototype count as names too.
- The list of names changes when you add or delete a property.
- Inside
with, an object can hide some properties from this lookup with a special property calledSymbol.unscopables.
These rules are one more reason to write order.price in everyday code.
const order makes an ordinary binding in a declarative record. That binding just holds an object. The with (order) statement is what makes a separate Object Environment Record, which offers the object's properties as names. Putting an object in a variable doesn't make an Object Environment Record.
The global record also uses an Object Environment Record. There, the object is the global object (the object you reach with globalThis), not order. So the same kind of record shows up in two places. The spec's object record rules describe the property lookup and the special with rules.
The two parts of a global record
The Global Environment Record holds the names declared at the top level of your code, outside every function and block. It is one record made of two parts:
- An object part. This is an Object Environment Record that uses the global object. Top-level
varandfunctiondeclarations go here, so they become properties you can read withglobalThis. - A declarative part. This is a plain list of names. Top-level
let,const, andclassdeclarations go here, so they are not properties of the global object.
These rules are for classic scripts: plain script files, such as a <script> tag without type="module". Modules follow different rules, which come later in this lesson.
var cart = "tea";
function checkout() { return cart; }
let price = 3;
const user = "Asha";
console.log(globalThis.cart, globalThis.checkout());
console.log(globalThis.price, globalThis.user);Line 1 declares cart with var. Line 2 declares the function checkout. Lines 3 and 4 declare price with let and user with const. Line 5 prints tea tea, because cart and checkout are properties of the global object. Line 6 prints undefined undefined, because price and user are not. But reading price by its name still gives 3.
var and function names go on the global object; let and const names go in a separate list that globalThis can't see.This example only works as a top-level classic script. If a bundler, a function, or a module wraps the code, the names aren't top-level any more, and the result changes. The lesson's test runs it as a fresh script. There, globalThis is that script's own global object, not a browser window. In the browser editor, run the block as a script, not as a module.
One more point: reading a missing property gives undefined. So globalThis.price being undefined doesn't mean the name price is missing. It only means price isn't a property of the global object.
The next box is shortened from the spec's global record steps. In it, [[DeclarativeRecord]] is the declarative part and [[ObjectRecord]] is the object part. The double square brackets are just how the spec names a part.
Global HasBinding(name): check [[DeclarativeRecord]] first.
If not found, check [[ObjectRecord]].
CreateGlobalVarBinding(name, deletable): create and initialize in [[ObjectRecord]].In plain words, line by line:
- To check whether a top-level name exists, look in the declarative part first.
- If it isn't there, look in the object part, which means the global object's properties.
- A top-level
vargoes in the object part, and it gets its first value,undefined, right away.
So a top-level var ends up as a property of the global object, and a let doesn't.
The table sums up where each kind of top-level declaration goes. Read each row from left to right.
| When you write | Where the name goes | What you can see |
|---|---|---|
A top-level var in a classic script | The object part of the global record. | It becomes a property of the global object, so globalThis.cart works. |
| A top-level function declaration in a classic script | The object part of the global record. | It becomes a property of the global object, so globalThis.checkout works. |
A top-level let or const in a classic script | The declarative part of the global record. | It is not a property. globalThis.price is undefined, but price still works. |
| Any top-level declaration in a module | The module's own record. | It is not a property of the global object, not even a var. |
A top-level function declaration goes in the object part too. The spec has a separate step for it, CreateGlobalFunctionBinding. You can read every step in the spec's section on Global Environment Records.
Each call gets its own record and this
Each time you call a function, the spec makes a new Function Environment Record for that one call. It holds the function's parameters and the names declared at the top of its body. For a normal function (not an arrow function), it also holds this. Which object becomes this depends on how you call the function. The Reference type lesson explains that part.
"use strict";
function showOrder() { return this.order; }
const cart = { order: "tea", showOrder };
console.log(cart.showOrder());
console.log(showOrder.call({ order: "coffee" }));Line 1 turns on strict mode. That way, this never quietly becomes the global object. Line 2 makes a function that returns this.order. Line 3 puts that function on cart. Line 4 calls it through cart, so this is cart and it prints tea.
Line 5 uses .call(). A function's .call() method runs the function and lets you pick this: its first argument becomes this. Here that is { order: "coffee" }, so it prints coffee. Each of the two calls gets its own function record, with its own this.
const cart = {
order: "tea",
show() { return () => this.order; },
};
const readOrder = cart.show();
console.log(readOrder());Line 2 gives cart an order of tea. Line 3 is a method, show, that returns an arrow function. An arrow function has no this of its own. Line 5 calls show through cart, so inside that call this is cart. Line 6 calls the arrow and prints tea. The show call has already finished, but the arrow still uses that call's this.
In the spec, each record answers the question "do you have your own this?" with a step called HasThisBinding. A normal function's record answers yes. An arrow function's record answers no. Another step, GetThisEnvironment, starts at the current record and goes outward until it reaches a record that answers yes.
A function record also holds what super and new.target need for that call. The next lesson, about calls, covers those.
Imports read the current value
Each module file gets a Module Environment Record. It is a declarative record for the names declared at the top of the module, plus the names it imports.
An imported name works differently from a normal name. It doesn't hold a copy of the value. Instead, it points to the binding inside the module that exported it. The spec calls this an indirect binding. Two rules follow:
- The importing module can't give the imported name a new value.
- The exporting module can change its own exported
let, and importers see the new value.
export let price = 3;
export function changePrice() { price = 4; }Line 1 exports price, which starts at 3. Line 2 exports changePrice, a function that sets that same price to 4. This file is meant to be imported, so it doesn't run on its own here. The next file imports it.
import { price, changePrice } from "./cart.mjs";
console.log(price);
changePrice();
console.log(price);Line 1 imports both names from cart.mjs. Line 2 prints 3. Line 3 calls changePrice(), which runs inside cart.mjs. Line 4 prints 4. The lesson's test runs these as two real module files in Node, because a plain script can't use import. For the full story of how modules load, see module records.
page.mjs does not get its own copy of price. Each read looks in cart.mjs's box, so after changePrice() it sees 4.The next box is shortened from the spec's module record steps. targetModule is the module that exports the name (cart.mjs), and targetName is the exported name (price).
CreateImportBinding(name, targetModule, targetName): create an immutable indirect binding.
GetBindingValue(name, true): for an indirect binding, read targetEnv.GetBindingValue(targetName, true).In plain words, line by line:
- For
import { price }: addpriceto the importing module's list. Instead of a value, store where to find it: the namepriceincart.mjs. The importer can't change it. - When code reads
price: go tocart.mjsand read itspriceas it is right now.
So every read gets the current value. Nothing was copied.
Two more facts about modules:
- A module record's outer record is the global record. But the module's own top-level names stay in the module record.
- At the top level of a module,
thisisundefined.
You can read these steps in the spec's module record rules.
Private names in a class
A class can have private names, such as #price. Only code written inside that class can use them. The spec tracks a class's private names in a PrivateEnvironment Record.
Its job looks like an Environment Record's job, but it is a separate tool in the spec. It only handles names that start with #. It never holds normal names like price.
class Cart {
#price = 3;
total() { return this.#price; }
}
console.log(new Cart().total());Line 1 starts the Cart class. Line 2 declares a private field, #price, with the value 3. Line 3 reads it inside the total method. Line 5 makes a new cart, calls total(), and prints 3.
A normal property called price would be a different thing from #price. And code outside the class can't write cart.#price to read it.
The next box is shortened from the spec's private name steps. In it, [[Names]] is the record's list of private names, and [[OuterPrivateEnvironment]] is a link to the private record of the class around this one, if there is one.
NewPrivateEnvironment(outer): record [[OuterPrivateEnvironment]] and an empty [[Names]] list.
ResolvePrivateIdentifier(env, name): search [[Names]], then [[OuterPrivateEnvironment]].In plain words, line by line:
- When a class starts, make a new private record. Give it an empty list of private names and a link to the class around it.
- To find a private name like
#price, look in this class's list first. If it isn't there, follow the link outward.
This search uses its own links. It never uses the outer links that normal names follow, which you'll see in the next section. The spec explains this in its PrivateEnvironment Records section. You never make these records yourself in app code.
Don't think of #price as a normal property whose name happens to start with #. It is special class syntax. That is why cart.price can't reach it. The record says which class owns the name #price. Each cart object still has its own value for that field.
Following the outer link
Every normal Environment Record has an outer link. It points to the record of the code around it, or to null when there is nothing around it. The spec calls this link [[OuterEnv]].
To find a name, the spec uses two steps:
- ResolveBinding starts the search at the record of the code that is running right now.
- GetIdentifierReference asks one record: "do you have this name?" If the answer is no, it follows that record's outer link and asks again.
const price = 3;
function checkout() {
const order = "tea";
console.log(order, price);
}
checkout();Line 1 makes price outside the function, with the value 3. Line 3 makes order inside checkout. Line 4 finds order in the function's own record. It doesn't find price there, so it follows the outer link and finds price outside. Line 6 calls checkout, which prints tea 3.
The search doesn't hand back the value straight away. It hands back a Reference Record: an address that says which record has the name. The value is read from that address next.
price, JavaScript checks checkout's own record first. It isn't there, so JavaScript follows the outer link to the global record and finds 3.The next box is shortened from the spec's name lookup steps. In it, env is the record being checked, and HasBinding(name) asks that record whether it has the name.
ResolveBinding(name): start at the running context's LexicalEnvironment.
GetIdentifierReference(env, name, strict): ask env.HasBinding(name).
If found, return a Reference Record based on env.
Otherwise, try env.[[OuterEnv]]; at null, return an unresolvable Reference Record.In plain words, line by line:
- Start with the record of the code that is running now.
- Ask this record: do you have the name?
- If yes, stop and hand back an address that points into this record.
- If no, follow the outer link and ask that record. If the link is
null, hand back an address that says "not found".
If no record has the name, the search ends with a "not found" address. Reading from that address later can throw a ReferenceError.
If a nearer record has the name but it isn't ready yet, the search still stops there, and reading it throws. This is what makes the TDZ work. If the search kept going outward, it would wrongly find an outer name with the same spelling.
A replay of a small lookup model written for this lesson. It is not an engine debugger, and it doesn't show the engine's real records.
script
function checkout() { const order = "tea"; return order + ": " + price;}console.log(checkout());This replay runs a small model of the lookup rules, written for this lesson, and saves a snapshot at each step. It doesn't pause real JavaScript or show the engine's real records. Step until order is found in the function. Then watch the search follow the outer link to find price. The replay's code prints tea: 3.
const price = 3;
function checkout() {
const order = "tea";
console.log(order, price);
}
checkout();order = teaprice = 3globalcheckout has no price of its own, so the search follows the outer link to the global record.
The lesson's model finds price in the global record. Its value there is 3.
The playground changes one thing: whether checkout also has its own price = 4. The code panel always shows the version without it. The checkbox only changes the lesson's model, not the code shown. Without the inner price, the search finds 3 in the global record. With it, the search stops in checkout and finds 4. Reset takes you back to the first case.
You look for a contact on your own phone before you ask someone at home. If your phone already has that name, you use the number you saved. A contact with the same name on the family list doesn't change the number you found.
- In real life: Search your own phone's contacts first
- In JavaScript: Ask the current record if it has the name
- In real life: If the name isn't there, ask the family's shared list
- In JavaScript: Follow the outer link and ask again
- In real life: Stop at the first matching name
- In JavaScript: A nearer name hides an outer one with the same spelling
A cart that remembers its price
Say a shop website makes a cart with a price, and the page reads that price later. A factory function (a function that makes and returns a new object) can do this. When the factory call ends, a function it returned can still read the factory's names.
In the spec, each function remembers the record it was created in. That keeps the record reachable. The engine's garbage collector (the part that frees memory nobody can reach any more) decides how and how long to store it.
function makeCart(price) {
return { total() { return price; } };
}
const cart = makeCart(3);
console.log(cart.total());Line 1 starts makeCart, which takes a price parameter. Line 2 returns an object with a total method that returns price. Line 4 makes a cart with the price 3. Line 5 prints 3. This is useful when checkout needs a saved setting, and you don't want it to be a global property that any code could change.
function makeOrder() {
let price = 3;
return () => ++price;
}
const nextPrice = makeOrder();
console.log(nextPrice());
console.log(nextPrice());Line 2 makes price with let, so it can change. Line 3 returns an arrow function that adds 1 to price and returns the new value. Line 5 keeps that function in nextPrice. Line 6 prints 4 and line 7 prints 5. Both calls use the same price binding. Neither call starts from a fresh copy. A function that keeps using names from where it was created is called a closure.
A replay of the real makeOrder function. It doesn't look inside the engine's memory.
script
function makeOrder() { let price = 3; return () => ++price;}console.log(nextPrice());console.log(nextPrice());This second replay calls the real makeOrder function and records each change to price. Back and Reset only move through the saved snapshots. They don't show how the engine really stores price, or when it frees it.
let priceinside a blockconst orderinside a block- An imported
price - Top-level
var cartin a classic script priceinsidewith (order)#pricedeclared in a class
Sort each example by the kind of record that holds its name.
Common mix-ups
Code that looks alike can follow different lookup rules. Before you guess what code prints, ask three questions:
- Is this a classic script or a module?
- Does the name belong to a block?
- Am I reading a plain name, like
price, or an object property, likeglobalThis.price?
const price = 3;
{
const price = 4;
console.log(price);
}
console.log(price);Line 1 makes an outer price with the value 3. Line 3 makes an inner price with the value 4. Line 4 prints 4, and line 6 prints 3. Nothing changed the outer value. Inside the braces, the search simply starts at a different record. When a nearer name hides an outer name like this, it is called shadowing.
- "All variables are properties of an object." Names in a declarative record, like
letnames, are not properties of any object. - "Reading a
lettoo early gives the outer value." The search stops at the nearer name, which isn't ready, so the read throws. - "An import copies the value." An import points to the exporting module's binding and reads its current value.
- "A closure copies all the names around it." The function keeps using the same live bindings. Engines may store them however they like.
- "Private names like
#priceare found like normal names." They are found through a separate chain of private records.
The table lists every kind of record in this lesson. Read each row from left to right: the kind, what it holds, and an example.
| Kind of record | What it holds | Example |
|---|---|---|
| Declarative | Names kept straight in a list, with no object behind them. | A block's let price. |
| Object | Names that are really the properties of one object. | price inside the old with (order) statement. |
| Global | Two parts: an object part (the global object) and a declarative part (a plain list). | The top-level names of a classic script. |
| Function | A plain list for one call. A normal function's record also holds that call's this. | The parameters of one call, and this in cart.showOrder(). |
| Module | A plain list for one module file. Imported names in it point to another module. | import { price } in page.mjs. |
| PrivateEnvironment | Only private names that start with #. It is kept apart from all the other records. | A class's #price. |
globalThis.price reads a property. A plain price searches the records for a name. In the same classic script, the two can give different results. Neither one lets your code see the record itself. Records exist only in the spec's rules; there is no way to inspect one from JavaScript.
Practice
Use the same three steps each time:
- Find the nearest record.
- Check whether it has the name. If not, follow the outer link.
- Once you find the name, check whether it is ready to read.
For a property read, like globalThis.price, ask which object the property belongs to instead. These tasks use code and output from this lesson.
A checkout block reads price too early, catches the error, and then reads it again. Type both printed lines in order. Then explain to yourself why the first line is not undefined.
{
try { console.log(price); }
catch (error) { console.log(error.name); }
let price = 3;
console.log(price);
}The two lines are ReferenceError and 3. The first read finds the block's own price, which has no value yet, so it throws. After line 4 gives it 3, the second read works.
The code has one price outside a block and another inside it. Guess the two printed lines in order. Don't assume the inner one changes the outer value.
const price = 3;
{
const price = 4;
console.log(price);
}
console.log(price);It prints 4 and then 3. The inner price hides the outer one inside the braces, but it never changes it.
A classic script declares a cart, a checkout function, a price, and a user. Name the kinds of declaration that become properties of the global object. Check your answer against the two printed lines.
var cart = "tea";
function checkout() { return cart; }
let price = 3;
const user = "Asha";
console.log(globalThis.cart, globalThis.checkout());
console.log(globalThis.price, globalThis.user);Top-level var and function declarations go in the object part, so they become global object properties. Top-level let and const go in the declarative part. That is why the two lines are tea tea and undefined undefined.
nextPrice is called twice, after makeOrder has finished. Guess both printed prices. Explain why the second call doesn't start again from 3.
function makeOrder() {
let price = 3;
return () => ++price;
}
const nextPrice = makeOrder();
console.log(nextPrice());
console.log(nextPrice());It prints 4 and 5. The returned function keeps using the same price after makeOrder has finished, so each call adds 1 to it.
A Cart class hides its price in #price. Name the kind of spec record that tracks that private name. Then check what total() prints for a new cart.
class Cart {
#price = 3;
total() { return this.#price; }
}
console.log(new Cart().total());A PrivateEnvironment Record tracks #price. The field's value is 3, but the name #price is not found through the normal outer links.
A shop website imports price from its cart module. During checkout, the cart module changes its exported price. Which kind of record holds the name in the importing module? Explain why a second read can see the new price.
import { price, changePrice } from "./cart.mjs";
console.log(price);
changePrice();
console.log(price);The importing module's Module Environment Record holds price. That name points to the exporter's price and can't be changed by the importer. The exporter can change its own price, so the page reads 3 and later 4.
Check your understanding
To guess what code prints, keep three things apart: the name, the record that holds it, and its value. A property read like globalThis.price works differently from a plain name like price. The quiz has both kinds.
Question 1 of 7What is an Environment Record?
Choose an answer to see the explanation.
Question 2 of 7What does this code with two prices print?
Read the code, then predictconst price = 3; { const price = 4; console.log(price); } console.log(price);Choose an answer to see the explanation.
Question 3 of 7Why does reading a
letname before its line throw?Choose an answer to see the explanation.
Question 4 of 7In a classic script, what does this print for a top-level
let?Read the code, then predictlet price = 3; console.log(globalThis.price);Choose an answer to see the explanation.
Question 5 of 7What does this arrow function print?
Read the code, then predictconst cart = { order: "tea", show() { return () => this.order; } }; console.log(cart.show()());Choose an answer to see the explanation.
Question 6 of 7The exporting module changes its exported
price. What does the importing module see?Choose an answer to see the explanation.
Question 7 of 7A name isn't in the current record. Where does JavaScript look next?
Choose an answer to see the explanation.
If you miss a question, go back to the matching example above and run it again. Remember: a TDZ error happens after the search finds the nearer name. It doesn't mean the name couldn't be found.
Key takeaways
Finding a name is not always a property search on an object. The spec uses different records for different kinds of names:
- normal
letandconstnames, - names that come from an object's properties,
- top-level names in a classic script,
- names inside a function call,
- names in a module,
- and private names in a class.
These records explain what your code does. They don't say how an engine must store anything.
- A
letorconstname exists before it gets its first value. Reading it too early throws aReferenceError. - In a classic script, top-level
varandfunctionnames go on the global object.letandconstnames don't. - A normal function call's record can hold
this. An arrow function goes outward to findthis. - An import points to the exporting module's binding, so it sees the exporter's changes.
- Private names like
#priceare found through a separate chain of private records. - To find a name, JavaScript checks the nearest record first. It follows the outer link only if the name isn't there.
One line to remember.
An Environment Record tells the spec which binding a name means, and where to look next if the name isn't there.
Coming next: Execution contexts & jobs in the spec. That lesson shows which record the running code starts its search from, and how the spec decides what code runs when.