Course outline · 0% complete

0/29 lessons0%

Course overview →

let and const

lesson 2-1 · ~8 min · 4/29

Naming values so you can reuse them

Lesson 1-3 built sentences out of variables and a template literal, wrapped in backticks so that ${...} placeholders were filled in rather than printed literally. Those variables were declared with let, and that keyword was introduced without explanation.

This lesson explains it. JavaScript has two keywords for creating a variable, they differ in exactly one way, and choosing between them is a decision you will make in every program you write.

Two ways to declare a variable

In Python a variable appears the moment you assign it: score = 10. JavaScript makes you declare it first, and gives you two keywords:

  • let declares a variable you may reassign later.
  • const declares a variable you may never reassign. The name is locked to its value.
let score = 10;
score = 15;        // fine

const name = "Ada";
name = "Grace";    // TypeError! const cannot be reassigned

The professional habit: use const by default, and switch to let only when you actually need to reassign (counters, running totals). This tells every reader which values can change.

You may also see var in old tutorials. It is the pre-2015 keyword with confusing scope rules. Modern code does not use it, and neither will we.

let score1015reassign okconst name"Ada"locked, no reassignment
A let variable can point at a new value later. A const variable is locked to its first value forever.

A counter and a fixed label

A score changes as the game goes on, so it is declared with let. A greeting never changes, so it is declared with const. Reading those two keywords tells you which value moves before you read another line.

let score = 10;
score = score + 5;
console.log(score);

const greeting = "Welcome back";
console.log(greeting);

Output

15
Welcome back

The line score = score + 5; has no keyword in front of it, because score already exists. The keyword appears once, when the variable is created, and every later assignment is a bare name on the left of the =.

Fixing an assignment to a constant

The program below throws TypeError: Assignment to constant variable. The declaration and the reassignment disagree: points is created as a const, then line two tries to give it a new value.

const points = 0;
points = points + 10;
console.log(points);

Changing the keyword on the first line to let makes the declaration match how the variable is actually used, and the program prints 10:

let points = 0;
points = points + 10;
console.log(points);

Output

10

The error message points at line two, but the mistake lives on line one. That is worth internalizing: when JavaScript complains about an assignment to a constant, the fix is almost always in the declaration, not in the line that failed.

Choosing a keyword for a running total

A shopping total that grows as items are added needs let, because it gets reassigned on every single addition.

const would throw TypeError: Assignment to constant variable. on the first update, since a const name is locked to the value it was given. var would technically work, but it is the pre-2015 keyword with confusing scope rules and modern code has retired it. And unlike Python, JavaScript really does require a keyword the first time a name appears, so leaving it off entirely is not an option either.

The habit to build: reach for const first, then downgrade to let the moment you find yourself needing to reassign. A reader who sees let knows to watch that value, and a reader who sees const knows they can stop worrying about it.