Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Declaring Variables · Lesson 9 of 15

const

A free lesson from Swipe Coding · JavaScript, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

Sometimes a value should never change once it's set — think of the number of days in a week, or a configuration value your program relies on. JavaScript lets you declare this kind of variable with const. A const variable cannot be reassigned after its initial value is set, and — unlike let — it must be given a value at the very moment it's declared.

const daysInWeek = 7;
console.log(daysInWeek); // 7
daysInWeek is initialized immediately, since const requires a value right at declaration.
🤖
Try to reassign a const and JavaScript won't let you forget it. 🚫
const daysInWeek = 7;
daysInWeek = 8; // TypeError: Assignment to constant variable.
Reassigning a const throws an error as soon as this line runs.
🤖
const-by-default is one of those habits that quietly prevents whole categories of bugs. 🛡️

Many JavaScript developers reach for const by default, and only switch to let when they know a variable truly needs to be reassigned later. This makes code easier to read: seeing const tells anyone reading it that the value is locked in for good, which helps prevent accidental reassignment bugs.

Fill in the blank

By default, many JavaScript developers prefer ____ over let, and only switch when a variable needs to be reassigned.

Here's the critical catch: const only protects the variable binding itself — it does not freeze the value's contents. If a const variable holds an object or array, you cannot reassign the variable to a different object or array, but you can still change what's inside it.

const scores = [10, 20];
scores.push(30);
console.log(scores); // [10, 20, 30]
push() mutates the array in place — this is allowed because the array itself isn't being reassigned, only its contents change.
Quick check

Which of these is allowed for an array assigned to a const variable?

True or false

Because scores is declared with const, calling scores.push(30) will throw an error.

Chapter quiz

Declaring Variables

10 questions · pass with 60% — just like in the app.

Quick check

Which keyword declares a variable whose value can be reassigned?

True or false

In JavaScript, you must declare a variable's type before you can use it, similar to Java.

Fill in the blank

JavaScript variable names conventionally use ____, e.g. totalScore.

Quick check

What happens to a variable declared with let inside an if block's {} once that block ends?

True or false

A variable declared with let only exists within the nearest enclosing pair of curly braces {}.

Quick check

Which statement about const is true?

Fill in the blank

Trying to reassign a variable declared with const causes a ____ error.

True or false

Once created, the contents of an array assigned to a const variable can never be changed.

Quick check

Why do many JavaScript developers prefer const by default?

Quick check

What happens when this code runs?

const scores = [10, 20];
scores.push(30);

Keep your streak going in Swipe Coding

Swipe through every level of JavaScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →