Keeping Names Inside Their Braces

The port of backup_check.py now declares each name once, with const by default and let for the status line and the warnings count. Next comes the line the sysadmin reads first each morning: “Backing up to kestrel-db-backups in eu-west-1 (dry run)”. Its mode is chosen in an if and else, and that’s where a Python habit runs into a JavaScript rule about where a name lives.

Port the Mode Choice

The Python check assigns mode in each branch and prints it after the if. That works because a Python if has no scope of its own:

Language: Python
bucket = "kestrel-db-backups"
region = "eu-west-1"
dry_run = True

if dry_run:
    mode = "dry run"
else:
    mode = "live"

print(f"Backing up to {bucket} in {region} ({mode})")

In JavaScript, the Booleans are lowercase true and false, and the condition goes in parentheses with each branch in braces, since JavaScript doesn’t read indentation. Each branch sets the mode once, so a line-by-line port uses const. Predict what this prints:

Language: JavaScript
const bucket = "kestrel-db-backups";
const region = "eu-west-1";
const dryRun = true;

if (dryRun) {
  const mode = "dry run";
} else {
  const mode = "live";
}

console.log(`Backing up to ${bucket} in ${region} (${mode})`);

The first branch runs, yet the check stops before the line the sysadmin needs:

Language: Program Output
ReferenceError: mode is not defined

The braces around an if body, an else body, a loop body, or a bare { } mark a block. A name declared with let or const inside a block can’t be reached after its closing brace. That’s block scope.

Each mode lives in its own branch’s block, and console.log() sits outside both. Code inside a block can still read outer names such as bucket, much as a Python function reads a module-level name. The difference is that every JavaScript block makes a scope, and a Python if or loop body doesn’t.

Assigning works differently too. A bare mode = ... inside a block updates an outer mode declared with let, whereas a Python function would make a new local unless it uses global or nonlocal. A let or const inside the braces makes a new name in that block instead.

Declare the Mode Where You Use It

The fix moves the declaration up to the level where mode is read, then assigns inside each branch with no keyword:

Language: JavaScript
const bucket = "kestrel-db-backups";
const region = "eu-west-1";
const dryRun = true;

let mode;
if (dryRun) {
  mode = "dry run";
} else {
  mode = "live";
}

console.log(`Backing up to ${bucket} in ${region} (${mode})`);

Now mode and the print line share a scope, and the port matches Python:

Language: Program Output
Backing up to kestrel-db-backups in eu-west-1 (dry run)

let mode; has no =, so JavaScript gives mode the value undefined until a branch assigns it. You need let because a const declaration requires an initial value. Here’s how the two languages place the name:

Python JavaScript
nothing before the if let mode; before the if
mode = "live" in a branch mode = "live";, no keyword
warnings += 1 in a branch warnings += 1;, no keyword

You’ll use the last row for the warnings count. Watch out for let warnings = warnings + 1; inside a block. The let makes a second warnings that lives only in the braces. Inside them, the right-hand warnings already means that new one, which has no value yet, so the script stops with “ReferenceError: Cannot access ‘warnings’ before initialization”. Drop the let to update the outer count. Why the new name exists before its line runs comes later.

In JavaScript, you declare a name at the level where it’s read, even when a branch is the first place that sets it, and you update outer names with a bare assignment or +=.

Next up is Count the Check’s Warnings, an exercise with a teammate’s port of the top of the check. The port should count a warning for each missing setting, then print the mode and the number of warnings, and you’ll get it doing exactly that.

Become a Member to join the conversation.