2.2 Constants
Welcome to this video where we talk about constants in JavaScript. Let's start by declaring a variable named interestRate with the let keyword and assigning it a value. Once initialized, the variable can be reassigned later in the program — that's exactly the purpose of a variable. If we set it to a new value of 1 and then log it, the console prints 1 because the script runs from top to bottom and the second assignment overwrites the first.
When the value must never change
In real applications, however, there are values that we never want to change after their initial assignment. Letting them be modified by mistake would create subtle bugs. For these situations, instead of declaring a variable, we declare a constant. As its name suggests, a constant's value cannot change after initialization.
To declare a constant, simply replace let with const:
const interestRate = 0.3;
interestRate = 1; // ❌ TypeError: Assignment to constant variable.
When you try to reassign interestRate, JavaScript throws a TypeError: Assignment to constant variable. The runtime refuses the reassignment because the binding is read-only after the first value.
- Use
constby default for values that should never change. - Use
letonly when you really need to reassign the variable later.
That's it for constants. In the next video, we will talk about primitive types in JavaScript.
Summary
This lesson introduces JavaScript constants and the difference between mutable variables (declared with `let`) and immutable constants (declared with `const`). The instructor demonstrates how changing a variable's value affects program execution, then explains why constants are essential in real applications to prevent accidental value modifications that could introduce bugs. Attempting to reassign a constant throws a TypeError, protecting developers from unintended changes to critical values.
Key points
- Variables declared with `let` can be reassigned at any time, while constants declared with `const` cannot be changed after initialization
- Constants prevent bugs in real applications by ensuring certain values remain fixed throughout program execution
- Attempting to reassign a constant throws a TypeError with the message 'Assignment to constant variable'
- Program execution flows top-to-bottom, so variable reassignments only affect the value at that point and beyond
- In production code, use `const` for values that should remain immutable, such as configuration values or settings
FAQ
What's the difference between `let` and `const` in JavaScript?
`let` declares a mutable variable that can be reassigned at any time, while `const` declares a constant whose value is fixed after initialization. Constants help prevent accidental modifications that could cause bugs in your application.
What error do you get when trying to reassign a constant?
A TypeError is thrown with the message 'Assignment to constant variable'. This error prevents you from accidentally changing a value that should remain fixed and immutable.
When should you use `const` instead of `let`?
Use `const` for any value that shouldn't change during program execution, such as configuration values, interest rates, or other fixed settings. This makes your code more robust and prevents unintended modifications to critical values.