Home JavaScript JavaScript const

JavaScript const

Beginner ⏱ 7 min read Updated: Aug 2026
  • const declares a variable that cannot be reassigned
  • const variables are block-scoped, just like let
  • A const variable must be given a value at the time it is declared
  • Objects and arrays declared with const can still have their contents edited
  • const is not the same as making a value fully unchangeable (immutable)
  • It is the recommended default choice when a value will not be reassigned

What Is const in JavaScript?

The const keyword declares a variable that is meant to stay the same after it is created. It behaves much like let in terms of scope, but it adds one extra rule: once a value is assigned, that variable cannot be pointed at a new value later in the program.

Important: A const variable must be initialised immediately; you cannot declare it and assign a value on a later line.

Basic Example of const

Example

<script>
const siteName = "CodingRoute";
console.log(siteName);
</script>
            
Result
CodingRoute

If you tried to run siteName = "NewName"; after this, JavaScript would throw an error, because siteName was declared as a constant.

Key Characteristics of const

Block-Scoped

Exists only inside the block where it was declared, same as let.

No Reassignment

Attempting to assign a new value to a const variable causes an error.

Must Initialise

A value must be provided at the moment of declaration.

Editable Contents

Objects and arrays declared with const can still change internally.

const with Objects and Arrays

A common point of confusion is that const does not make an object or array fully frozen. It only stops the variable name from being reassigned to a completely different object or array. This means you can still add items to a const array or change a property inside a const object, because you are editing the contents, not replacing the variable itself.

Example

<script>
const user = { name: "Rahul" };
user.name = "Priya";
console.log(user.name);
</script>
            
Result
Priya

Why Developers Prefer const

Many developers now use const as their default choice for every new variable and only switch to let when they know the value will genuinely need to change. This habit makes code easier to read, because seeing const immediately tells you that a value will stay fixed, reducing the chance of accidental reassignment elsewhere in the program.