Keeping Names Inside Their Braces

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

In Python, an if has no scope of its own, so a name assigned in the branch that runs is still there after it. In JavaScript, where you declare a name determines which lines can reach it.

Choose the Mode Inside if and else

The Python check picks the mode from the dry-run flag. Each branch assigns mode, and the print line after the if statement uses it:

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})")

This works because mode belongs to the module and both branches assign it, so it’s set whichever one runs.

For now, the port holds the settings as given values. JavaScript writes its Booleans in lowercase, true and false, so the flag becomes const dryRun = true;. Where Python ends the condition with a colon and indents each branch, a JavaScript if puts its condition in parentheses and wraps each branch’s body in braces. Each branch sets the mode once and never changes it, so const looks like the right keyword for a line-by-line port:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.