← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 9 of 12

JS Const

Think of a sealed shipping container, once it's packed and sealed at the factory, you can't swap out the whole container for a different one, but if it happens to be full of individually movable crates, you can still rearrange or open those crates inside. const works the same way for variables, once you declare a const, you can never reassign it to point at a different value entirely, but if that value is an object or array, its contents can still be changed. const protects the binding, not necessarily the whole value. On real projects like cookiescursor.com, defaulting to const wherever possible makes code easier to trust at a glance, because you immediately know that name will never point somewhere else.

Constant Values

A const variable's binding is locked in place the moment it's declared, meaning you can never point that same variable name at a different value later in the script.

Note: Use const as your default choice for any variable, only switch to let when you discover the value genuinely needs to change.

Warning: Attempting to assign a new value to a const variable, even one of the same type, throws a TypeError immediately.

Example: Constant Values

javascript
const maxUsers = 100;
// maxUsers = 200; // TypeError: Assignment to constant variable
console.log(maxUsers);

const Must Be Initialized

Because a const variable's value can never change after declaration, JavaScript requires you to provide that value immediately, declaring a const without one is not allowed.

Note: If you find yourself wanting to declare a variable without an initial value, that's usually a sign you actually need let instead of const.

Warning: Writing const withoutValue; on its own line causes a SyntaxError before the script even starts running.

Example: const Must Be Initialized

javascript
const pi = 3.14159;
// const withoutValue; // SyntaxError: Missing initializer in const declaration
console.log(pi);

const With Objects

When a const variable holds an object, the variable itself cannot be reassigned to a new object, but the properties inside that object can still be freely added, changed, or removed.

Note: const on an object is a promise that this variable will always refer to the same object, not a promise that the object's contents never change.

Warning: Developers new to const are sometimes surprised that modifying an object's property doesn't throw an error, only reassigning the variable itself does.

Example: const With Objects

javascript
const user = { name: "Amit" };
user.name = "Priya"; // allowed, modifying a property
// user = {}; // TypeError, cannot reassign the variable
console.log(user);

const With Arrays

The same rule applies to arrays declared with const, the array's elements can be added, removed, or changed using array methods, but the variable can never be reassigned to point at a completely different array.

Note: Methods like push and splice modify an existing array in place, which is why they work fine on const arrays.

Warning: Using the assignment operator to replace an entire const array, like myArray = [], fails, even though modifying it with push does not.

Example: const With Arrays

javascript
const numbers = [1, 2, 3];
numbers.push(4); // allowed
// numbers = []; // TypeError, cannot reassign
console.log(numbers);

When to Use const

const should be the default choice for the vast majority of variables in modern JavaScript, using it consistently signals to anyone reading the code exactly which values are meant to stay fixed and which, marked with let, are expected to change.

Note: Try writing new code with const everywhere first, and only change specific variables to let when the code genuinely fails to run without reassignment.

Warning: Overusing let purely out of caution, even for values that never actually change, makes it harder to tell at a glance which variables are truly dynamic.

Example: When to Use const

javascript
const taxRate = 0.08; // never changes
let price = 100;
console.log(price + price * taxRate);
Common Mistakes
  1. Trying to reassign a const variable directly, which always throws a TypeError, regardless of the value's type.
  2. Assuming const makes an object or array completely unchangeable, when only the variable's binding is actually protected.
  3. Declaring a const without immediately giving it a value, which is not allowed since const requires initialization at declaration.
Chapter Summary
  • const creates a variable whose binding cannot be reassigned after it's declared.
  • const requires a value at the moment of declaration, unlike let which can be declared without one.
  • Objects and arrays declared with const can still have their internal contents modified, only reassignment of the variable itself is blocked.
Browser Support

const has been supported in every major browser since 2015 and is considered a fully standard, safe part of modern JavaScript.

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.