A Variable That Won't Change
JavaScript const
The `const` keyword declares a block-scoped binding that cannot be reassigned. Default to `const` — use `let` only when you actually need to reassign.
What you'll learn
- Declare constants with `const`
- Understand the difference between a constant binding and a constant value
- Default to `const` in new code
const declares a binding that cannot be reassigned. Once you
attach a value to a const name, that name keeps pointing at the same
value for the rest of its scope.
const TAX_RATE = 0.07;
console.log(TAX_RATE); // 0.07
// TAX_RATE = 0.10; // TypeError: Assignment to constant variable. Default to const
Modern JavaScript convention: use const everywhere by default,
and only use let when you actually need to reassign.
Why? Reading code is easier when you can trust that a name keeps
pointing at the same thing. const is a signal: “this won’t change.”
Binding vs Value
A const makes the binding constant — the name keeps pointing
at the same value. It does not make the value itself immutable.
If the value is an object or array, you can still change what’s
inside it.
const user = { name: "Ada", age: 36 };
user.age = 37; // ✅ allowed — mutating a property
user.email = "a@b.c"; // ✅ allowed — adding a property
console.log(user); // { name: 'Ada', age: 37, email: 'a@b.c' }
// user = { name: "Lin" }; // ❌ TypeError — reassigning the binding To actually freeze an object’s properties, use Object.freeze().
We’ll cover that in the Objects chapter.
Block Scope (Same as let)
Like let, a const only exists inside its block:
{
const message = "inside";
console.log(message);
}
// console.log(message); // ReferenceError Must Initialize
Unlike let, a const must be given a value when you declare it.
const pi = 3.14159; // ✅
// const x; // ❌ SyntaxError: Missing initializer in const declaration When let Wins
Stick with let only when:
- You’re counting (
let count = 0; count++;). - You’re reassigning inside a loop or conditional.
- You’re swapping or pivoting a value.
Everywhere else, const is your friend.
Up Next
You can now store values. Time to do something with them — operators.
JavaScript Operators →