Declaring Variables with let and const
What Is a Variable?
A variable is a named container for a value. You give a value a name, and later you can use or change that value by referring to the name. Think of it like a labeled box: the label is the variable name, and the contents are the value.
In JavaScript you create variables with the keywords let or const. A keyword is a word with special meaning in the language.
let score = 0;
const maxScore = 100;let: Values That Change
Use let when the value will be updated later. let lets you reassign the variable, which means giving it a new value.
let year = 1995;
year = year + 1;
console.log(year); // 1996Here, year starts at 1995. The line year = year + 1 takes the old value, adds 1, and stores the result back in year.
const: Values That Stay
Use const (short for constant) when the value should never be reassigned. If you try, JavaScript stops your program with an error.
const language = 'JavaScript';
language = 'Python'; // TypeError: Assignment to constant variable.Important: const stops reassignment, but it does not make the value itself unchangeable. For example, you can still add items to an array declared with const. What you cannot do is point that variable at a brand-new value.
Comparing let and const
| Keyword | Can reassign? | Use when... |
|---|---|---|
| `let` | Yes | The value will change, like a counter or score |
| `const` | No | The value is fixed, like a name or a maximum |
const language = 'JavaScript';
let year = 1995;
year = year + 1;
console.log(`${language} was created in ${year - 1}`);The last line uses a template literal: backticks with ${...} slots that insert variable values into a string. You will meet template literals again soon, but you can read them as "replace this with the value."
Building Strings from Variables
Variables become much more useful when you combine them into messages. Template literals make this easy and readable.
const name = 'Ada';
let visits = 3;
console.log(`Welcome back, ${name}. You have visited ${visits} times.`);If visits changes later, the same code can print an updated message.
Which One Should I Use?
A common habit among JavaScript programmers: default to const, and switch to let only when you know the value must change. This makes your intent clear — anyone reading const knows that name will not be reassigned.
There is also an older keyword, var, that you may see in older code. It behaves differently in subtle ways, and modern code avoids it. For new code, stick with let and const.
Try It Yourself
Open your browser's developer console (usually F12, then the Console tab) and try:
const planet = 'Earth';
let moons = 1;
moons = moons + 1;
console.log(`${planet} has ${moons} moons in this example.`);Now try to reassign planet. You should see the error described above — that is the safety net working.
Exercise
Which keyword would you choose for a variable that stores a user's age, which gets updated on their birthday?
Show answer
let, because the value changes over time. Use const only for values that are never reassigned.
Exercise
What happens if you write const colors = ['red']; then colors.push('blue');?
Show answer
It works. const prevents reassigning the variable, but the array's contents can still be modified.
Spotted a mistake, or code that does not work?