Skip to main content

How to Declare a Constant with const in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Most variables in a program are set once and never pointed anywhere else. Declaring them with const says so, and JavaScript then stops any line that tries to reassign them, which catches a whole class of mistakes early.

Syntax​

Syntax
const <name> = <value>;
PartWhat it does
constDeclares a variable that can't be reassigned.
<name>The variable's name.
<value>Required: a const must get its value in the same line.

MDN's const reference: "The value of a constant can't be changed through reassignment using the assignment operator, but if a constant is an object, its properties can be added, updated, or removed."

The Example​

The example declares a city and a list of visited cities with const. It adds to the list, which works, and then tries to reassign the city, which doesn't.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which declares const city = Paris and prints it, declares const visited with Paris, pushes Rome and prints the list, then tries city = Rome.

main.js
const city = 'Paris';
console.log(city);

const visited = ['Paris'];
visited.push('Rome');
console.log(visited);

city = 'Rome';

Step 2: Run It​

Terminal
node main.js
Output
Paris
[ 'Paris', 'Rome' ]
/Users/Shared/you/main.js:8
city = 'Rome';
^

TypeError: Assignment to constant variable.
at Object.<anonymous> (/Users/Shared/you/main.js:8:6)
at Module._compile (node:internal/modules/cjs/loader:1968:14)
at Object..js (node:internal/modules/cjs/loader:2108:10)
at Module.load (node:internal/modules/cjs/loader:1690:32)
at Module._load (node:internal/modules/cjs/loader:1480:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
at node:internal/main/run_main_module:33:47

Node.js v26.13.1

A zsh terminal where node main.js prints Paris and the list Paris, Rome, then stops at line 8 with TypeError: Assignment to constant variable.

  • visited.push('Rome') worked: const keeps visited on the same array, but the array itself can change.
  • city = 'Rome' tried to point city at a new value, and Node.js stopped with TypeError: Assignment to constant variable., showing the line and the column.

The command is the same in zsh, bash and PowerShell.

When to Use let​

Use let for a variable that really does change, such as a counter or a running total:

let
let city = 'Paris';
city = 'Rome';
console.log(city); // Rome

A good habit is const by default and let only where you reassign.

Common Mistakes​

MistakeWhat happensDo this instead
Reassigning a constTypeError: Assignment to constant variable.Use let for that variable
Declaring a const without a value, as in const x;SyntaxError: Missing initializer in const declarationGive it its value in the same line
Expecting const to freeze an object or arrayIts contents can still changeUse Object.freeze() when the contents must not change

Troubleshooting​

SymptomLikely causeFix
TypeError: Assignment to constant variable.A line assigns a new value to a constChange the declaration to let, or stop reassigning
SyntaxError: Missing initializer in const declarationconst has no = valueAdd the value

FAQ​

Is const faster than let?​

Don't choose for speed. Choose const because it tells readers, and JavaScript, that the variable won't be reassigned.

Where can I use a const variable?​

Only inside the block, the { ... }, where you declared it. Outside that block it doesn't exist.

Conclusion​

const declares a variable that can't be reassigned, so a stray = fails loudly. The value inside, such as an array, can still change; use let when the variable itself must change.

Additional Resources​