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
const daysInWeek = 7; daysInWeek = 8; // TypeError: Assignment to constant variable.
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.
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]
Which of these is allowed for an array assigned to a const variable?
Because scores is declared with const, calling scores.push(30) will throw an error.
Declaring Variables
10 questions · pass with 60% — just like in the app.
Which keyword declares a variable whose value can be reassigned?
In JavaScript, you must declare a variable's type before you can use it, similar to Java.
JavaScript variable names conventionally use ____, e.g. totalScore.
What happens to a variable declared with let inside an if block's {} once that block ends?
A variable declared with let only exists within the nearest enclosing pair of curly braces {}.
Which statement about const is true?
Trying to reassign a variable declared with const causes a ____ error.
Once created, the contents of an array assigned to a const variable can never be changed.
Why do many JavaScript developers prefer const by default?
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 →