A developer turns the crank of a HITS tally counter beside a SCORE arcade cabinet, with MAX_LIVES hearts in a sealed case, a locked CONST panel, and an unlocked LET panel.

JavaScript const vs let: When to Use Each

by Martin Breuss Updated Reading time estimate 10m basics js

In the const vs let choice, start with const, and use let only for a name that has to change. A let tells anyone reading your code that a value might change later, even when it never does. Every example is a small file you run with node, so keep a terminal open.

Start with the numbers in a tiny terminal game: Which of them ever change?

Free Bonus: JavaScript Cheat Sheet

JavaScript Cheat Sheet

Variables, arrays, objects, functions, and async basics in one printable reference.

Get the Free Cheat Sheet »

Declare Fixed Values With const

A game has values that change while it runs and values that never do. Say your game counts the player’s hits and has a fixed number of lives:

Language: JavaScript Filename: game.js
const MAX_LIVES = 3;
let hits = 0;

hits += 1;
hits += 1;
console.log(`Hits: ${hits}`);
console.log(`Max lives: ${MAX_LIVES}`);

Save it as game.js and give it a spin:

Language: Shell
$ node game.js
Hits: 2
Max lives: 3

The const keyword declares a name that gets its value once and keeps it. MAX_LIVES is a setting, so const fits. hits goes up with every hit, so it needs let, likely the keyword you used when you first learned to name values in JavaScript.

The rule of thumb is to use const by default and let only for names that change. Don’t take it too far, though. A score that changes is one name declared with let, so don’t declare a new const for each step, like score1, score2, and score3.

Name Your Constants

You may have noticed that MAX_LIVES is written in uppercase snake case, with uppercase words separated by underscores, while hits uses camel case, which starts lowercase and capitalizes each subsequent word. Using const doesn’t make a name uppercase on its own. The common JavaScript convention, used in the Google JavaScript Style Guide and on javascript.info, splits const names into two groups:

  • Uppercase snake case for fixed settings whose value you write into the code up front and that never change, such as MAX_LIVES = 3 or API_URL = "https://example.com". Most of these live at the top of a file or module.
  • Camel case for every other name, including const names whose value the program works out while it runs, such as a total computed from other values.

Every const name keeps its value, but an uppercase name also tells readers, “this is configuration you can rely on.” You’ll see both groups side by side in the game that calculates finalScore.

Read the Two const Errors

Because a const name can’t get a new value later, JavaScript insists on a value when you declare it. Leave the value out, and Node.js refuses to run the file:

Language: JavaScript Filename: lives.js
const MAX_LIVES;

Run it, and you get a SyntaxError:

Language: Shell
$ node lives.js
...
SyntaxError: Missing initializer in const declaration
    ...

To fix it, give the setting its value on the same line, as in const MAX_LIVES = 3;. A fixed setting is known up front, so that’s where its value belongs.

The second error shows up when your code tries to reassign a constant. Say you follow the new rule too eagerly and declare hits with const:

Language: JavaScript Filename: hits.js
const hits = 0;
hits += 1;
console.log(`Hits: ${hits}`);

Run it, and the program stops at hits += 1:

Language: Shell
$ node hits.js
...
TypeError: Assignment to constant variable.
    ...

Whoops! The TypeError tells you that hits += 1 tries to assign a new value to a constant. The fix is to change const to let, because hits is a name that changes.

The button below opens a short chat with your AI mentor, where you can practice the rule. The mentor describes a name from the game and whether the program changes it. You answer const or let with a reason, and the mentor tells you whether your reason holds:

Check yourself before moving on:

  1. A game declares const level = 1; and later runs level += 1;. Which error do you get, and how do you fix it?
  2. Why does const MAX_LIVES; fail when let hits; works?
  1. You get a TypeError: Assignment to constant variable. because level changes. Declare it with let.
  2. A const name can never get a value later, so JavaScript needs its value right away. Without one, it’s a SyntaxError.

Choose const vs let for Each Name

In a whole program, you check each name against the lines that use it. This game counts three hits and works out a score. Before you open the answer, fill each blank with const or let and decide why:

Language: JavaScript
___ MAX_LIVES = 3;
___ hits = 0;

console.log("Game on!");
hits += 1;
hits += 1;
hits += 1;

___ finalScore = hits * 10 + MAX_LIVES * 5;
console.log(hits);
console.log(finalScore);

Each name gets the declaration its lines call for:

Language: JavaScript Filename: game.js
const MAX_LIVES = 3;
let hits = 0;

console.log("Game on!");
hits += 1;
hits += 1;
hits += 1;

const finalScore = hits * 10 + MAX_LIVES * 5;
console.log(hits);
console.log(finalScore);

Run it:

Language: Shell
$ node game.js
Game on!
3
45
  • MAX_LIVES is a fixed setting, so it gets const and an uppercase snake case name. You can still read it in a calculation. JavaScript only rejects attempts to give a const variable a new value.
  • hits gets a new value on three lines, so it needs let.
  • finalScore gets const too, because a const can take its value from any expression. Node.js works it out once when the line runs and stores the result, 45, in finalScore.

You might wonder why finalScore is written in camel case when it’s a const, just like MAX_LIVES. They hold different kinds of values. MAX_LIVES is a setting that you write into the code up front, and it’s the same every time the game runs, so it gets an uppercase snake case name.

The value of finalScore isn’t known until the program runs. It depends on how many hits the player scored, so it keeps a camel case name. const only promises that a name won’t get a new value. The uppercase name adds that the value is fixed configuration.

Read What Runs Before Each Error

The two const errors stop a program at different moments. Say the game only prints a misses counter at first, so const misses = 0; follows the rule. Then you add an update. Predict what this version prints before the error:

Language: JavaScript Filename: misses.js
const misses = 0;

console.log("Game on!");
misses += 1;
console.log(misses);

Node.js prints one line, then stops at the update:

Language: Shell
$ node misses.js
Game on!
...
TypeError: Assignment to constant variable.
    ...

Game on! prints first. misses += 1 has valid syntax but fails only when Node.js reaches it, so the lines above it have run. Now that misses changes, declare it with let.

A missing initial value causes a different error, even with a print above it:

Language: JavaScript Filename: no-value.js
console.log("Game on!");
const MAX_LIVES;

Run it:

Language: Shell
$ node no-value.js
...
SyntaxError: Missing initializer in const declaration
    ...

This time, not even Game on! prints. Node.js reads the whole file before running it, and const MAX_LIVES; has invalid syntax, so the SyntaxError stops everything.

A TypeError waits until Node.js reaches the reassignment, so only logging calls above it produce output. In hits.js earlier, no console.log() call came before hits += 1, so only the error showed.

For more practice, ask your AI mentor .

Check yourself by predicting what prints before each error:

  1. You add console.log("Ready!"); as the first line of hits.js from earlier. What prints before the error, and why?
  2. You add the same line above const MAX_LIVES; in lives.js. What prints now?

With the new first line, hits.js looks like this:

Language: JavaScript Filename: hits.js
console.log("Ready!");
const hits = 0;
hits += 1;
console.log(`Hits: ${hits}`);

Ready! prints before the error, because hits += 1 fails only when Node.js reaches it:

Language: Shell
$ node hits.js
Ready!
...
TypeError: Assignment to constant variable.
    ...

In lives.js, only the SyntaxError appears, as with no-value.js. Node.js reads the whole file first and stops before running any line.

The Bottom Line

You now have a rule for declaring names in JavaScript: start with const, and reach for let only when a name has to change. Fixed settings written into the code get uppercase snake case names like MAX_LIVES, and everything else, including const names for values computed at runtime, uses camel case.

You also know how to read the two const errors:

  • A SyntaxError: Missing initializer in const declaration stops the whole file before any line runs. Put the value right in the declaration.
  • A TypeError: Assignment to constant variable. stops the program only at the line that reassigns the name, so everything before it still runs. Switch that name to let.

The next time you reach for let, ask yourself whether the program ever gives that name a new value. If it doesn’t, const is the better fit.

To firm up the basics around these examples, check out this RealJS learning path. It covers variables, functions, loops, arrays, and objects, with exercises that check your code as you go:

Learning Path

JavaScript Basics

9 Resources ⋅ Skills: Running Code With Node, Variables, Text, Comparisons, Conditionals, Functions, Loops, Arrays, Objects, Debugging

Frequently Asked Questions

These are the questions people ask most often about const and let, with short answers:

Default to const. Switch a name to let only when your program assigns it something else later, like a counter that goes up during a game. Picking const tells readers the value stays put.

It means a line tried to reassign a name declared with const. Node.js raises it only when execution reaches that line, so any output above it still appears. Declaring the name with let instead fixes it.

A const name can never receive a value afterward, so JavaScript requires one up front. Leaving it out triggers a SyntaxError before a single line of the file runs.

Only some of them. By convention, fixed settings typed straight into the code, such as MAX_LIVES, use uppercase snake case. A const whose value comes from a calculation at runtime, like finalScore, stays in camel case.

Yes. Any expression works as the starting value of a const. Node.js evaluates it once when the declaration runs and keeps the result, which is how finalScore gets its value in the game example.

Free Bonus: JavaScript Cheat Sheet

Get a JavaScript Cheat Sheet (PDF), a printable reference to variables, types, strings, arrays, objects, functions, classes, modules, and async basics:

RealJS JavaScript Cheat Sheet pages stacked on top of each other

About Martin Breuss

Martin is Head of Content Strategy at RealJS. He came to programming from biology and has been teaching programming for more than ten years, as a coding mentor, code reviewer, curriculum developer, bootcamp instructor, and instructional designer.

» More about Martin

Master Real-World JavaScript Skills With Unlimited Access to RealJS

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert JavaScript developers:

Level Up Your JavaScript Skills »

Master Real-World JavaScript Skills
With Unlimited Access to RealJS

Locked learning resources

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert JavaScript developers:

Level Up Your JavaScript Skills »

What Do You Think?

Rate this article:

Keep Learning

Related Topics: basics js