TExcelTAD
Home › JavaScript › JS Const

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:

js
const PI = 3.14159;  // ✅
const rate;          // ❌ SyntaxError: Missing initializer in const declaration

Cannot be reassigned

js · try it yourself
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.

js · try it yourself
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):

js · try it yourself
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:

js · try it yourself
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?

  1. list.push(3);
  2. list[0] = 9;
  3. list = [3];
  4. list.length = 0;

What does const x; produce?

  1. x is undefined
  2. x is null
  3. A SyntaxError
  4. x is 0

How can you prevent an object's properties from being changed?

  1. Declare it with const
  2. Use Object.freeze()
  3. Use let
  4. Wrap it in an array