JS Const
In this page:
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
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
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
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
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
const taxRate = 0.08; // never changes
let price = 100;
console.log(price + price * taxRate);
- Trying to reassign a const variable directly, which always throws a TypeError, regardless of the value's type.
- Assuming const makes an object or array completely unchangeable, when only the variable's binding is actually protected.
- Declaring a const without immediately giving it a value, which is not allowed since const requires initialization at declaration.
- 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.
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: