JavaScript Const
const declares a variable that cannot be reassigned. Like let, it is block-scoped. Because most values in a program are set once and never replaced, const is the most common declaration in modern JavaScript.
Must be initialised
A const needs its value immediately — const x; on its own is a syntax error:
const PI = 3.14159; // ✅
const rate; // ❌ SyntaxError: Missing initializer in const declarationCannot be reassigned
const taxRate = 0.2;
try {
taxRate = 0.25;
} catch (err) {
console.log(err.name + ": " + err.message);
}
console.log(taxRate);TypeError: Assignment to constant variable.
0.2
Constant binding, not constant value
This is the most important thing to understand about const: it locks the variable, not the value. If the value is an object or array, you can still change what's inside it — you just can't point the variable at a different object.
const cart = ["apple"];
cart.push("bread"); // ✅ changing the array's contents
cart[0] = "green apple"; // ✅
console.log(cart);
const user = { name: "Lee", age: 30 };
user.age = 31; // ✅ changing a property
user.email = "lee@example.com";
console.log(user);
try {
user = { name: "Someone else" }; // ❌ replacing the object
} catch (err) {
console.log(err.message);
}[ 'green apple', 'bread' ]
{ name: 'Lee', age: 31, email: 'lee@example.com' }
Assignment to constant variable.
Think of a const as a label permanently glued to one box. You can rearrange what's in the box, but you can't move the label to another box.
Truly frozen objects
If you want an object whose properties can't change either, use Object.freeze(). Changes are then rejected (in strict mode — which playgrounds and modules always use — they throw):
const config = Object.freeze({ theme: "dark", fontSize: 14 });
try {
config.fontSize = 20;
} catch (err) {
console.log(err.message);
}
console.log(config.fontSize);
console.log(Object.isFrozen(config));Cannot assign to read only property 'fontSize' of object '#<Object>'
14
true
Object.freeze is shallow: objects nested inside a frozen object can still be modified.
Block scope
const follows the same block-scope and temporal-dead-zone rules as let. A fresh const is created each time a block runs, so it's perfect inside loops:
const prices = [4, 10, 25];
for (const price of prices) {
const withTax = price * 1.2;
console.log(price, "->", withTax);
}4 -> 4.8
10 -> 12
25 -> 30
(A classic for (let i = 0; …; i++) counter must be let, because i++ reassigns it.)
When to use what
| Situation | Choose |
|---|---|
| A value set once (numbers, strings, functions, objects, arrays you only mutate) | const |
| A counter, accumulator or value replaced later | let |
| Maintaining old code | var (but consider refactoring) |
Naming tip: many teams write "magic" configuration values in UPPER_SNAKE_CASE (const MAX_RETRIES = 3;) but ordinary const variables in camelCase.
Test yourself
Given const list = [1, 2];, which line throws an error?
list.push(3);list[0] = 9;list = [3];list.length = 0;
What does const x; produce?
- x is undefined
- x is null
- A SyntaxError
- x is 0
How can you prevent an object's properties from being changed?
- Declare it with const
- Use Object.freeze()
- Use let
- Wrap it in an array