Loading video player…

Knowing When a Name Exists

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:

Language: JavaScript
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):

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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.

00:00 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.

00:11 Python keeps its price table at the top and its calls at the bottom, which print 18 for Ana, then 11 for Ben.

00:19 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.

00:34 What does the port log when you run it? Your turn: pause the video and predict.

00:44 It logs 18, then stops with a ReferenceError.

00:49 It can’t access PRICES before initialization, so Ben’s price never logs.

00:56 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.

01:08 The call worked, and the body failed.

01:13 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.

01:24 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.

01:38 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.

01:48 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 one either.

02:00 It’s Python’s UnboundLocalError, but a let inside an if covers only that block, not the whole function.

02:09 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.

02:21 Run it again: it logs Ana’s and Ben’s prices, as Python does. Watch out: written as an arrow in a const, the shoe helper is a const, so calling it above its line stops with the same error.

02:36 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.

02:51 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.

Become a Member to join the conversation.