let and const
What you'll learn:
- store values in
letvariables andconstconstants, and choose between them; - find out a value's type with
typeof; - explain where
0.30000000000000004,InfinityandNaNcome from in calculations; - tell
undefinedfromnull.

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:
console.log(250 * 3); // 750
console.log(250 * 3 + 100); // 850Here 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:
let price = 250;
console.log(price * 3); // 750
price = 300;
console.log(price * 3); // 900Let's go through the example line by line:
let price = 250;creates the variablepriceand puts250in it.console.log(price * 3);readsprice, multiplies it by3and prints750.price = 300;puts the new value300inprice.- The same
console.log(price * 3);now prints900.
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:
- The program reads the old value of
priceand adds50to it. - 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.
1let price = 250;2const count = 3;3let total = price * count;4console.log(total);5price = 300;6console.log(total);7total = price * count;8console.log(total);9total = total + 100;10console.log(total);
let creates the variable price and puts 250 in it.
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.
const count = 3;
let total = 250 * count;
total = total + 100;
console.log(total); // 850Let's go through the example line by line:
const count = 3;— the count does not change, so it is a constant.let total = 250 * count;puts the number750intotal.total = total + 100;reads750, adds100and stores850back.console.log(total);prints850.
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
letvariable can be given a new value, and aconstconstant cannot; - a constant is declared with its value right away, while
letcan also be declared without one.
Common mistake
A value that has to change was declared with const:
const count = 1;
console.log("start");
count = count + 1; // ✗
console.log(count);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
letdeclares a variable that can be given a new value, andconstdeclares a constant whose value is set once.- The assignment
=replaces the old value with a new one, andx = x + 1reads the old value first. - A variable holds a ready value, not a formula: after
pricechanges,totaldoes not recompute by itself. - Write
constby default, andletwhen 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.