Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript5 tutorials
  • Node.js Introduction2 tutorials
  • JavascriptJavascript · Beginner · Mid
  • JavascriptJavascript · Beginner · Short
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 4

Declaring Variables with let and const

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.
Level: BeginnerLength: Mid2 min read

Read at another depth

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.

Code example in javascript
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.

Code example in javascript
let year = 1995;
year = year + 1;
console.log(year); // 1996

Here, 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.

Code example in javascript
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

KeywordCan reassign?Use when...
`let`YesThe value will change, like a counter or score
`const`NoThe value is fixed, like a name or a maximum
Code example in javascript
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.

Code example in javascript
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:

Code example in javascript
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?

Report an issue

Chapter: Declaring Variables with let and const

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that