The desk laptop’s front_desk.py has to move to the tablet app, one piece at a time. The two smallest pieces move first: the price helper that adds shoe hire and the counter that tracks how many climbers came through the door today.
Both rely on names defined outside their functions. In Python, reading such a name takes nothing extra, but rebinding a module-level name from inside a function takes a global statement. Here you’ll see how a JavaScript function finds outer names and why it can change them without any extra keyword.
Read Names From Around the Function
Shoe hire costs £4 on top of the day pass, kept in a module-level constant. Here’s the Python helper:
SHOE_HIRE = 4
def with_shoes(price):
return price + SHOE_HIRE
print(with_shoes(14))
The JavaScript port keeps the same shape, with const for the constant and braces around the body:
const SHOE_HIRE = 4;
function withShoes(price) {
return price + SHOE_HIRE;
}
console.log(withShoes(14));
Ana Reyes pays for an adult pass with shoes:
18
withShoes() doesn’t declare SHOE_HIRE, so JavaScript looks for it outward: first in the function, then in whatever surrounds it, up to the top of the file. That chain of nested scopes is the scope chain, and it does the job of Python’s LEGB lookup through local, enclosing, global, and built-in scopes. Like LEGB, it goes by where the function is written and ignores where it’s called.
The one addition is that block braces create a scope around an if, a loop, or a function body. A variable declared with let or const inside a block can’t be reached from outside it, whereas a Python variable assigned inside an indented block can still be read after the block.
The top of the file is called module scope. It’s much like a Python module’s globals. The difference is that any Python module can import another’s top-level names, while a JavaScript file keeps its names to itself unless it chooses to share them. How a file shares names comes later. Past module scope, the chain ends in the global scope, which holds JavaScript’s built-ins and Node’s console, much as LEGB ends in Python’s built-ins.
Update the Day’s Count From a Function
Reading carries over, but writing doesn’t. The script counts the day’s check-ins in a module-level variable, and check_in() adds one per climber:
checked_in_today = 0
def check_in(name):
global checked_in_today
checked_in_today += 1
return f"{name} is checked in"
print(check_in("Ana Reyes"))
print(check_in("Ben Okafor"))
print(f"Checked in today: {checked_in_today}")
Python needs that global line. Without it, assigning to checked_in_today makes the name local to check_in(), so += tries to read a local that has no value yet and raises UnboundLocalError.
JavaScript has no global statement and no nonlocal either. In a line-by-line port, global checkedInToday; isn’t valid JavaScript and stops the whole file with SyntaxError: Unexpected identifier 'checkedInToday' before anything runs. Delete the line, and before you run the port, predict what checkedInToday += 1 does without global:
let checkedInToday = 0;
function checkIn(name) {
checkedInToday += 1;
return `${name} is checked in`;
}
console.log(checkIn("Ana Reyes"));
console.log(checkIn("Ben Okafor"));
console.log(`Checked in today: ${checkedInToday}`);
There’s no error. The count goes up with each climber, exactly as the Python script’s does:
Ana Reyes is checked in
Ben Okafor is checked in
Checked in today: 2
This works because, in JavaScript, an assignment never creates a variable. Only a declaration does, such as let, const, or a parameter. When checkIn() assigns to checkedInToday, JavaScript walks the scope chain it uses for reading and updates the first declared checkedInToday, the let at the top of the file. If it found none, you’d get an error instead of a new variable, because the port runs as a module and modules always run in strict mode.
Declare Every Local
The same rule has a second side, which matters as the port grows and a helper reuses a name the file already declares at the top. In Python, assigning to a name inside a function makes a fresh local, whatever the module already has. In JavaScript, a bare assignment inside a function changes the nearest outer variable with that name, however far up the chain. If that’s a const, it’s an error, which is why the counter is a let.
So a JavaScript developer declares every local with const by default, or with let when the value changes later, even when an outer variable happens to share its name. The declaration makes a new variable in the block or function where it’s written, and the outer one stays untouched. A bare assignment then always means “change a variable that already exists.” In checkIn(), the missing let tells you the function updates the day’s count on purpose.
That leaves the question of when names come into existence. Python runs a def when it reaches it, so by convention its scripts define everything first and make the calls at the bottom. In the next lesson, Knowing When a Name Exists, you’ll see how JavaScript sets up a scope’s declarations before its first line runs, why that lets you call a function above its declaration, and where relying on it goes wrong in the desk port.