Loading video player…

Knowing When a Name Exists

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:

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

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];
}

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

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

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

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

00:15 Python keeps the day passes in a table at the top. price_for() returns one price, and the desk’s lines at the bottom call both helpers. Python runs a def when it reaches it, so a call above the def stops with a NameError.

00:31 Run in Python, the script prints 18 for Ana, then 11 for Ben.

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

00:51 The price section goes at the end of the file, as a new section. The table comes first, then the one function that reads it, kept beside it.

01:02 Python’s version prints 18, then 11. What does the JavaScript port log when you run it? Your turn: pause the video and predict.

01:17 It logs 18, then stops with a ReferenceError. It can’t access PRICES before initialization, so Ben’s price never logs.

01:30 Look at where the trace points. The source line Node prints first isn’t the call. It’s the return line, line 13, inside the function. Node lists the innermost frame first, the reverse of Python’s order, so under the function’s frame is the call, on line 8.

01:50 The call worked. The function was already running when JavaScript stopped.

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

02:11 Setting up the declarations first is called hoisting.

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

02:31 Line 10, the table’s own line, never ran.

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

02:48 Hoisting made the function callable. It didn’t make the table ready.

02:54 Python fails inside price_for() too, if it runs before the table’s line. JavaScript already knows the name, so it says “before initialization” instead.

03:05 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.

03:13 It’s Python’s UnboundLocalError, but Python locks the whole function, and a let only its block.

03:22 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.

03:39 Run it again, and it logs Ana’s and Ben’s prices, as Python does. Watch out: that’s true of function declarations only. Written as an arrow in a const, the shoe helper is a const, so calling it above its line stops with the same error.

03:58 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.

04:09 Next up is Reading Old Code That Uses var, a text lesson. The gym’s old booking widget is written with var, which sets its names up differently.

04:20 You’ll learn to read it, and catch the bug it hides.

Become a Member to join the conversation.