JavaScript Lesson 03 — What Do let and const Mean? Give Values a Name
Typing the same price three times is possible, but updating only two copies is an easy mistake. Giving the value a name lets several parts of the code refer to the same information.
Read a small calculation
const unitPrice = 120;
let quantity = 2;
console.log(unitPrice * quantity);
quantity = 3;
console.log(unitPrice * quantity);
unitPrice holds 120 and quantity initially holds 2. The first multiplication produces 240. After reassigning the quantity to 3, the second multiplication produces 360. Check both console lines.
The equals sign assigns the value on the right to the name on the left. It does not ask whether the two sides are equal. We will use a different operator for comparison in the conditions lesson.
Why use two declaration keywords?
We plan to reassign quantity, so we declare it with let. We do not reassign the unit price in this example, so const fits it. A const binding cannot be assigned a different value later.
Try this separately from the working file:
const unitPrice = 120;
unitPrice = 150;
The second line throws an error because the binding cannot be reassigned. It is not a universal “freeze everything” instruction. When we reach arrays, we will see a const binding whose array contents can still change.
Choose a name that explains its job
x and y may work, but unitPrice and quantity make the calculation easier to understand. Names are case-sensitive: quantity and Quantity are different. A variable name cannot contain a space.
Declaring a value does not automatically print it. We still need an operation such as console.log to inspect it.
Declaration is not reassignment
let quantity = 2;
quantity = 4;
console.log(quantity);
Let appears only on the first line. The next assignment changes the existing binding. Repeating the same let declaration in one scope is an error, not the normal way to update a value.
Const requires an initializer; const quantity; is invalid. Let can be declared without an initial value, whose meaning we will inspect in the data-types lesson.
Copying a number does not create a live link
let quantity = 2;
const copiedQuantity = quantity;
quantity = 5;
console.log(quantity);
console.log(copiedQuantity);
The results are 5 and 2. CopiedQuantity received the numeric value at assignment time. It did not become a live alias to the quantity binding. This is where a spreadsheet analogy would mislead us.
Arrays later introduce object-reference behavior. Do not generalize the numeric example to every kind of value; we will test that difference explicitly.
A binding has a scope
const message = 'Outside';
if (true) {
const message = 'Inside';
console.log(message);
}
console.log(message);
The inner log prints Inside; the outer prints Outside. The block has a separate binding with the same name, shadowing rather than reassigning the outer one. Avoid unnecessary shadowing while learning.
A name declared only inside the block cannot be read outside it. Scope means where a binding is accessible in code. We hold the condition true here so this experiment focuses on scope rather than branch selection.
What const actually guarantees
It prevents reassignment of that binding. It does not validate business rules or freeze all contents of an object. Pick it according to reassignment needs, not according to a vague idea that one keyword is always more modern.
Identify every reassignment in your price example. Use let where the exercise intentionally updates a binding and const where it does not.
A calculation is not a live spreadsheet formula
If you write const total = unitPrice * quantity; and then change the quantity, total keeps the number calculated earlier. That line does not automatically run again. Calculate a new total when you need the updated result.
Set the quantity to 5 and check for 600. Then deliberately change the capitalization of one occurrence of its name. Use the console techniques from lesson two to find the name that was never declared.
References: MDN