Knowing When a Name Exists
The tablet app needs the desk’s price lookup next, and adding it shows you when a name exists in
JavaScript. The port has the shoe-hire helper and the desk’s line for Ana. The Python script,
front_desk.py, keeps its price table, PRICES, at the top and its calls at the bottom, which print
18 for Ana, then 11 for Ben.
In JavaScript, a function declaration can be called above its line. So Ben’s line ports under Ana’s, and the price section goes at the end of the file: the table first, then the one function that reads it:
const SHOE_HIRE = 4;
function withShoes(price) {
return price + SHOE_HIRE;
}
console.log(withShoes(14));
console.log(priceFor("concession"));
const PRICES = { adult: 14, concession: 11, child: 9 };
function priceFor(kind) {
return PRICES[kind];
}
The port logs 18, then stops with a ReferenceError, so Ben’s price never logs (Node’s own frames and version line left out):
18
file:///.../front-desk.js:13
return PRICES[kind];
^
ReferenceError: Cannot access 'PRICES' before initialization
at priceFor (file:///.../front-desk.js:13:3)
at file:///.../front-desk.js:8:13
The trace doesn’t point at the call. It points at the return line, inside the function. Node
lists the innermost frame first, unlike Python, so the call on line 8 sits under it. The call
worked, and the body failed.
Before line 1 ran, JavaScript set up each declaration in the file. The functions came out ready to
call. The constants were set up too, but locked, with no value yet. That setup is called
hoisting. Then line 1 unlocked SHOE_HIRE, and line 8’s call found the price function ready. But
its body read PRICES, still locked.
A const exists from the top of its block, here the whole file, but can’t be used until its line
runs. That stretch is the temporal dead zone. Hoisting made the function callable, not the table
ready.
Watch out for the same lock inside a function: if report() declares its own let total, a line
above it can’t read the outer total either: Cannot access 'total' before initialization. It’s
Python’s UnboundLocalError, but a let inside an if covers only that block, not the whole
function.
The fix is Python’s order: the table moves up, and the desk’s lines move to the bottom. The functions keep their order, since a function declaration works anywhere once its constants are set:
const SHOE_HIRE = 4;
const PRICES = { adult: 14, concession: 11, child: 9 };
function withShoes(price) {
return price + SHOE_HIRE;
}
function priceFor(kind) {
return PRICES[kind];
}
console.log(withShoes(14));
console.log(priceFor("concession"));
Run it again, and it logs Ana’s and Ben’s prices, as Python does:
18
11
Watch out: written as an arrow in a const, const withShoes = (price) => price + SHOE_HIRE;, the
shoe helper is a const, so calling it above its line stops with ReferenceError: Cannot access
'withShoes' before initialization.
Hoisting doesn’t change the order a file runs in, so write it in that order: constants first, functions anywhere, and the calls that start the work last.
Next up: Reading Old Code That Uses var, a text lesson. The gym’s old booking widget uses var,
which sets its names up differently, and you’ll learn to read it and catch the bug it hides.