Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Declaring Names Once With const and let

Declaring Names Once With const and let

In this lesson, you ported the top of the backup check to JavaScript and found that JavaScript declares each name once, then assigns it.

  • In Python, a name exists as soon as you assign to it, and assigning again points it at a new value.
  • The port is backup-check.js in the folder backup-check/, whose package.json holds { "type": "module" }, so the file runs as a module, in strict mode.
  • Like Python, JavaScript reads the whole file before it runs any of it. A let declares a name, and a name can be declared only once, so a second let for it is a SyntaxError that stops every line, even the ones above it.
  • JavaScript keeps declaring and assigning apart: you declare a name once, with const or let, then assign it new values with no keyword.
  • Assigning to a name that was never declared stops a module with a ReferenceError when that line runs, where Python would create the name. That’s strict mode at work.
  • += works as in Python. warnings += 1; is short for warnings = warnings + 1;, so the name gets a new value.
  • That reassignment is why warnings stays a let in the finished port below, even though the lines that add to it come later.
  • Start every name as const, and use let only when the code really reassigns it. The keyword is a promise to the reader: the lets show at a glance which names change.
  • Python marks a constant with an UPPER_CASE name, but nothing at runtime stops a reassignment. A const is enforced.
  • Watch out: unlike the second let, which stopped the whole file, a const that’s reassigned gives a TypeError only when that line runs.
  • Watch out: a const fixes what the name points at, not the contents, so an array or object in it can still change, as a Python list can.
  • In older code, you’ll meet var, an earlier way to declare names. Write let and const instead.

Here’s the line-by-line port, where the second status line got its own let:

Language: JavaScript
let bucket = "kestrel-db-backups";
let region = "eu-west-1";
let notifyEmail = "[email protected]";
let warnings = 0;

let status = "checking settings";
console.log(`Status: ${status}`);

let status = "ready to back up";
console.log(`Status: ${status}`);

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

00:02 The shop’s servers are being rebuilt to run JavaScript, so the backup check needs a port. Start with the top of the Python script, which names its settings: the bucket, the region, and the address to notify, plus a warnings count.

00:17 Further down, the status changes once, and the script prints it each time. In Python, a name exists as soon as you assign to it, and assigning again points it at a new value.

00:29 JavaScript creates its names differently.

00:34 The port lives in a backup check folder, whose package.json sets type to "module" for every file. So the file runs as a module, in strict mode, where JavaScript turns more slips into errors.

00:48 Line by line, each Python assignment becomes a let line, with the name in camel case and a semicolon. The print lines port with console.log() and a template literal.

01:01 The second status assignment ports the same way as the first, keyword and all. The port has two console.log() lines, and Python printed two status lines.

01:14 What will JavaScript print when you run the file? Your turn: pause the video and predict.

01:24 Nothing at all, not even the first status line. JavaScript stops with a syntax error: the identifier status has already been declared.

01:36 The caret points at the second status, on line nine. The first console.log(), above it, never ran.

01:45 Like Python, JavaScript reads the whole file before it runs any of it: in Python, a typo on line two stops line one too. A let declares a name, and a name can be declared only once, so reading the file is enough to catch the second one.

02:04 Python creates a name by assigning to it. JavaScript keeps the two apart: you declare a name once, then assign it new values with no keyword. The declaration can’t be skipped, either: where Python would create the name, a reference error stops the module when that line runs.

02:21 That’s strict mode at work.

02:25 So the fix drops the second let from line nine, which becomes a plain assignment. Run the file again, and it prints both status lines, the same as Python.

02:38 When the Python check finds a problem, += adds one to the warnings count. It’s short for warnings = warnings + 1, so the name gets a new value.

02:51 JavaScript has the same operator, so the line ports with a semicolon added. That’s a reassignment, so the let stays in place.

03:01 These three values never change here. So change their keyword to const, for names that are never reassigned. Start every name as a const, and use let only when the code really reassigns it.

03:14 The keyword is a promise to the reader: at a glance, the lets show which names change. Python marks a constant with an upper case name, but nothing at runtime stops a reassignment.

03:28 A const is enforced.

03:32 Watch out: unlike the second let, which stopped the whole file, a const that’s reassigned gives a type error only when that line runs.

03:41 Watch out, too: a const fixes what the name points at, not the contents, so an array or object in it can still change, as a Python list can. In older code, you’ll also meet var, an earlier way to declare names.

03:58 Write let and const instead.

04:02 Declare each name once, with const by default and let for a name you reassign. After that, assign with no keyword.

04:12 Next up is the text lesson Keeping Names Inside Their Braces. You’ll see that a let or const lives only inside its nearest braces, and where to declare the check’s mode, dry run or live, so the print line can reach it.

Become a Member to join the conversation.