The two smallest pieces of front_desk.py move to the tablet app first: the shoe hire helper and the day’s check-in counter. Both use names defined outside their functions. Reading those names works as it does in Python. Writing them is where JavaScript parts ways, and it doesn’t need a global keyword to do it.
Read Outer Names Through the Scope Chain
The port of with_shoes() reads the module-level SHOE_HIRE exactly as the Python version does, so withShoes(14) still gives 18 for Ana Reyes’s adult pass. JavaScript looks up SHOE_HIRE outward through any enclosing blocks and functions to the top of the file. This scope chain is LEGB under another name, decided by where the function is written.
One stop is new to a Pythonista. The braces of an if or a loop make a scope, so a variable declared with let or const inside can’t be reached after the block, unlike a Python name assigned in an if or a for.
The top of the file is module scope. Unlike a Python module’s globals, which any importer can reach, a JavaScript module’s names stay private unless you choose to share them. You’ll do that later. Beyond module scope, the chain ends in the global scope, home to JavaScript’s built-ins and Node’s console, as LEGB ends in Python’s built-ins.
Update the Day’s Count From a Function
In Python, check_in() needs global checked_in_today before checked_in_today += 1, or the assignment makes the name local and raises UnboundLocalError. JavaScript has neither global nor nonlocal. Watch out for a line-by-line port: global checkedInToday; stops the whole file with SyntaxError: Unexpected identifier 'checkedInToday'. Without that line, the counter works:
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}`);
The count goes up with each climber:
Ana Reyes is checked in
Ben Okafor is checked in
Checked in today: 2
In a module, which is how this course runs JavaScript, an assignment never creates a variable. Only a declaration—let, const, or a function declaration—or a parameter makes one. So checkedInToday += 1 changes the nearest outer variable of that name, the one declared with let at the top. If you declare that variable with const, the assignment causes an error.
A typo is the likelier slip. checkedInTodya += 1 throws ReferenceError: checkedInTodya is not defined, because += reads the name first. A plain checkedInTodya = 1 throws the same error, since modules run in strict mode. Outside strict mode, that line would silently create a global.
The no-new-variable rule also cuts the other way. A Python helper that assigns to a name the module already has gets a fresh local. A JavaScript helper that assigns without let or const changes the outer variable. So declare every local variable in JavaScript, with const by default and let when you reassign it, even when an outer name matches. A bare assignment then always means “change a variable that already exists.”
Declaring a name is half the story. The other half is when it becomes usable. Python runs a def when it reaches it, so by convention you define functions first and call them at the bottom of your scripts. 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, and where relying on that goes wrong in the desk port.