Use const to create a JavaScript binding that cannot be reassigned, while remembering that the value it points to may still be mutable.
What it is
const declares a block-scoped variable whose binding is fixed after initialization. In other words, once a name is attached to a value, you cannot attach that same name to a different value later.
A useful mental model is to think of const as a permanent label, not a sealed box. If the label points to a primitive value such as a number or string, that value cannot be changed through the label. If the label points to an object, array, or function, the object itself can still be mutated unless you take extra steps such as using Object.freeze().
Related terms include binding, reassignment, mutation, block scope, and temporal dead zone. The temporal dead zone means that accessing a const variable before its declaration throws a ReferenceError.
Why it matters
- It makes code easier to reason about because a constant name will always refer to the same value.
- It helps prevent accidental reassignment bugs, especially in large functions or modules.
- It communicates intent: if a value should not change,
constsays so clearly. - It encourages safer defaults by making
leta deliberate choice for values that must change. - It works well with configuration objects, mathematical constants, and imported values.
Syntax or steps
The smallest useful pattern is to declare a constant and initialize it immediately:
const PI = 3.14159;
Because const bindings cannot be reassigned, you must provide a value at declaration time. This differs from let, which can be declared without an initializer and assigned later.
Use const when the variable name should not point to a new value. Use let when the variable needs to be reassigned, such as a loop counter or a value updated during execution.
Example
const TAX_RATE = 0.08;
const config = {
retries: 3,
timeout: 5000
};
function calculateTotal(price) {
const total = price + (price * TAX_RATE);
return total;
}
config.retries = 5; // allowed: mutating the object
// config = {}; // TypeError: Assignment to constant variable.
console.log(calculateTotal(100)); // 108
console.log(config.retries); // 5
Here, TAX_RATE is a constant primitive. It cannot be reassigned, and that is usually what you want for fixed values.
config is a constant binding to an object. The binding cannot change, so config = {} would throw an error. However, the object’s properties can still be changed, so config.retries = 5 is allowed.
Inside calculateTotal, total is a local constant. It is created each time the function runs and cannot be reassigned within that function call.
Common mistakes
- Thinking
constmakes objects immutable. It only prevents reassignment. UseObject.freeze()if you need shallow immutability. - Declaring
constwithout an initial value. This causes a syntax error. If you need to assign later, uselet. - Trying to reassign a constant in a loop. For example,
const i = 0; i = 1;fails. Useletwhen the variable must change. - Using
constfor values that genuinely change. If a variable is updated during execution,letis clearer and more correct.
When to use it
Prefer const as your default declaration. Reach for let only when reassignment is necessary. Avoid var in modern JavaScript unless you have a specific reason to use its older function-scoped behavior.
| Declaration | Best for | Key behavior |
|---|---|---|
const |
Values that should not be reassigned | Block-scoped, must initialize, no reassignment |
let |
Values that change during execution | Block-scoped, can be reassigned |
var |
Legacy code or special cases | Function-scoped, hoisted, easier to misuse |
Practice
Guided exercise: Declare const MONTHS = 12;. Then declare const user = { name: "Ada" };. Change user.name to "Grace", then try to reassign user to a new object. Notice that the property change succeeds, but the reassignment fails.
Challenge: Write a function called area that uses const PI = 3.14159; and returns the area of a circle for a given radius. Call area(2).
Expected output: 12.56636. Hint: Use const result = PI * radius * radius; inside the function.
Quick check
Question: Does const make an object immutable?
Answer: No. It prevents the variable from being reassigned, but the object’s properties can still be changed unless you freeze or otherwise protect the object.
Summary
const is a declaration that creates a non-reassignable binding, making it a strong default for values that should not change. It improves clarity and safety, but it does not automatically make objects immutable.