Skip to content
Theory · Variables and types · lesson 1 of 9

let and const

What you'll learn

What you'll learn:

  • store values in let variables and const constants, and choose between them;
  • find out a value's type with typeof;
  • explain where 0.30000000000000004, Infinity and NaN come from in calculations;
  • tell undefined from null.
A box labelled price holds a tile 300, while the old tile 250 leaves it

Picture a storeroom full of boxes. Each box has a label with a name on it, and inside lies one item. To get an item out, you don't need to remember which shelf the box is on: finding the right label is enough. A program needs the same thing — a place to put a value and to take it back by name.

Why is this needed? A program often uses one value in several places. If you write it as a number each time, that number repeats all over the code:

JavaScript
console.log(250 * 3);  // 750
console.log(250 * 3 + 100);  // 850

Here the price 250 is written twice. When the price changes, you will have to fix every line, and you risk missing one of them. A large program can have dozens of such lines. The value needs a name to read it by anywhere in the program.

Variables with let

A variable is a name under which a program stores a value. This is the labelled box: the name is the label, and the value is what lies inside. The word let declares a variable, and the = sign stores its starting value right away:

JavaScript
let price = 250;
console.log(price * 3);  // 750
price = 300;
console.log(price * 3);  // 900

Let's go through the example line by line:

  1. let price = 250; creates the variable price and puts 250 in it.
  2. console.log(price * 3); reads price, multiplies it by 3 and prints 750.
  3. price = 300; puts the new value 300 in price.
  4. The same console.log(price * 3); now prints 900.

The price is written in one place. If it changes, every line below reads the new value.

Assignment

The line price = 300; is an assignment: the = sign replaces the variable's old value with a new one. The word let is not written a second time, because the variable is already declared. When the program reads price, it gets the value stored last.

In a program, the = sign is not equality from math but a command: "store". That is why the right side of = can hold a calculation with the same variable. The line price = price + 50; runs in two steps:

  1. The program reads the old value of price and adds 50 to it.
  2. The result is stored back in price.

Here the box analogy stops working. A real item can be taken out of a box and set aside. An assignment sets nothing aside: the old value is gone, and there is no way to get it back.

One more detail confuses beginners most often. A variable holds a ready value, not the formula it was computed by. If you change price after let total = price * count;, total keeps the old result. Press "Next step" and watch which number lies in total after each line.

LiveVariable values line by linestep 1 of 10 · frames: 1

let creates the variable price and puts 250 in it.

Watch when total changes and when it does not

The value of total changed only on the lines where the program ran an assignment to total again.

Naming variables

Names use Latin letters and digits, and a digit cannot come first. A compound name is written as one word, with each following word starting with a capital letter: totalPrice, itemCount. A good name says what lies in the box: price is clearer than p.

Constants with const

The word const declares a constant — a variable whose value is assigned once, at the declaration. It is a box taped shut right after the item was put in. A constant can be read any number of times, but it cannot be given a new value.

JavaScript
const count = 3;
let total = 250 * count;
total = total + 100;
console.log(total);  // 850

Let's go through the example line by line:

  1. const count = 3; — the count does not change, so it is a constant.
  2. let total = 250 * count; puts the number 750 in total.
  3. total = total + 100; reads 750, adds 100 and stores 850 back.
  4. console.log(total); prints 850.

A constant cannot be declared without a value either. With the line const x; the program does not start at all and reports the error SyntaxError: Missing initializer in const declaration.

Choosing const or let

Declare a value with const by default, and use let for a variable that gets a new value later. The declaration then shows at a glance which values in the program change. When a reader of the code sees const, they know the value stays the same below.

Older code also has var — the earlier way to declare variables.

Interviewers like to ask this: "How does let differ from const?" The answer from this lesson has two parts:

  • a let variable can be given a new value, and a const constant cannot;
  • a constant is declared with its value right away, while let can also be declared without one.

Common mistake

A value that has to change was declared with const:

JavaScript
const count = 1;
console.log("start");
count = count + 1;  // ✗
console.log(count);
TEXT
TypeError: Assignment to constant variable.

The program manages to print start. Like ReferenceError, this error happens only when its line runs, and it stops the program there. If the value changes, declare the variable with let: let count = 1;.

Key points

  • let declares a variable that can be given a new value, and const declares a constant whose value is set once.
  • The assignment = replaces the old value with a new one, and x = x + 1 reads the old value first.
  • A variable holds a ready value, not a formula: after price changes, total does not recompute by itself.
  • Write const by default, and let when the value will change.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way