Knowing When a Name Exists
The tablet app needs the desk’s price lookup next. In this lesson, you’ll add it to the port and find out when a name exists in JavaScript. The port already has the shoe-hire helper and the desk’s line for Ana.
The Python script keeps the day passes in a table, PRICES, at the top beside shoe hire, and
price_for() returns one price. The desk’s lines at the bottom call both helpers:
SHOE_HIRE = 4
PRICES = {"adult": 14, "concession": 11, "child": 9}
def with_shoes(price):
return price + SHOE_HIRE
def price_for(kind):
return PRICES[kind]
print(with_shoes(14))
print(price_for("concession"))
It prints 18 for Ana, then 11 for Ben’s concession. Python runs a def when it reaches
it, so a call above the def stops with NameError: name 'price_for' is not defined. That’s
why its calls come last.
In JavaScript, a function declaration can be called above its line, so a helper doesn’t have to sit above the code that uses it. Ben’s line ports first, under Ana’s line. The price section goes at the end of the file, as a new section: the table first, then the one function that reads it, kept beside it. Here’s the port:
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];
}
Python’s version prints 18, then 11. The port logs 18, then stops, 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. The source line Node prints first is the return line, line 13, inside the function. Node lists the innermost frame first, the reverse of Python’s most recent call last, so under the function’s frame is the call, on line 8. The call worked: the function was already running when JavaScript stopped.
Before line 1 ran, JavaScript set up each declaration in the file. The two functions came out ready to call. The two constants were set up too, but locked, with no value yet. Setting up the declarations first is called hoisting.
Then the lines ran in order. Line 1 unlocked shoe hire, and line 7 logged Ana’s price. Line 8
called the price function, which was ready, and its body read PRICES, still locked, so
JavaScript stopped. Line 10, the table’s own line, never ran.
A const exists from the top of its block, here the whole file, but it can’t be used until its
line runs. That stretch is called the temporal dead zone. Hoisting made the function
callable. It didn’t make the table ready.
Python fails inside price_for() too, if it runs before the table’s line, with
NameError: name 'PRICES' is not defined. JavaScript already knows the name, so it says
“before initialization” instead.
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 Python locks the whole function, and
a let only its block.
The fix is the order Python’s file uses. The table moves up, and the desk’s two lines move to the bottom. The functions keep their order: a function declaration works anywhere, once the constants it reads 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: that’s true of function declarations only. 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 is Reading Old Code That Uses var, a text lesson. The gym’s old booking widget, from
2014, is written with var, which sets its names up differently. You’ll learn to read it, and
catch the bug it hides.