edubun
English
1 / 12

3 / 12 · Intro

Variables: let and const

A variable is a name for a value. After let score = 10, the word score stands for 10 until you give it something else.

Jump to the 10 practice questions

You will learn

  • Declare a variable with let or const and use it
  • Pick const by default and follow the naming rules

Before this: Values and console.log

1Explain

A name for a value

The line let score = 10; does two things. The keyword let declares a new variable called score, and = 10 puts the value 10 into it. The equals sign here does not mean "is equal to" as in maths. It means "store the value on the right in the name on the left." Programmers call this assignment. When score appears later without quotes, JavaScript looks up the box and uses the value inside.

Step through: after line 1 the name score stands for 10, and score + 5 uses that value without changing it.

  1. let score = 10;
  2. console.log(score); // 10
  3. console.log(score + 5); // 15
Variables
Output
10
15

2Explain

Changing a value with let

The line score = score + 5; looks impossible as maths, but read it as an instruction and it makes sense. JavaScript first evaluates the right side, the current score plus 5, which is 30. Then it stores 30 back into score. The old value is gone. This pattern, reading a variable and storing an updated value into it, is how programs count, add up totals and keep track of anything that changes.

Watch score: the right side is worked out with the old value, then the result is stored back.

  1. let score = 10;
  2. score = 25;
  3. console.log(score); // 25
  4. score = score + 5;
  5. console.log(score); // 30
Variables
Output
25
30

3Problem

Three assignments

What does this print?

let n = 2;
n = 10;
n = n * 3;
console.log(n);

Hint

Write n after each line: 2, then 10, then …

Solution

Answer: 30

n becomes 10, then 10 × 3 = 30.

4Explore

Copy, then change

b gets the value a has at that moment. Changing a afterwards leaves b as it was.

first value
new value
let a = 5;
let b = a;
a = 9;
console.log(a, b);
Output
9 5

5Problem

Does b change?

What does this print?

let a = 5;
let b = a;
a = 9;
console.log(b);

Hint

b got the number 5 when line 2 ran.

Solution

Answer: 5

b got a copy of the number 5. Changing a later does not change b.

6Explain

const for values that stay put

The keyword const also declares a variable, but it must be given a value straight away and can never be assigned a new one. Trying to do so stops the program with TypeError: Assignment to constant variable. That error is useful. If a value is not supposed to change, such as the number of days in a week or a tax rate, declare it with const.

const daysInWeek = 7;
console.log(daysInWeek * 4);   // 28
// daysInWeek = 8;  would stop with a TypeError
Output
28

7Problem

Assigning to a const

What happens when this runs?

const max = 5;
max = 6;

Hint

A const name can never be given a new value.

Solution

Answer: TypeError: Assignment to constant variable

A const variable cannot be assigned again; JavaScript stops with a TypeError.

8Problem

Which name is allowed?

A variable name may contain letters, digits, the underscore _ and the dollar sign $, but it cannot start with a digit and cannot contain spaces. So total2 is fine and 2total is not. Names are case sensitive: score and Score are two different variables. A few words such as let, const, if and for are reserved by the language and cannot be used as names.

Which is a valid variable name?

Hint

No digit first and no spaces.

Solution

Answer: secondPlace

Names cannot start with a digit or contain spaces.

9Practice

Add 5 to score

Add one line that increases score by 5, so the program prints 15.

Expected output
15
Your code
let score = 10;
// Add 5 to score on the next line.

console.log(score);
Hint

score + 5 on its own changes nothing. Store the result: score = score + 5; or score += 5;

A solution
let score = 10;
score = score + 5;
console.log(score);

10Practice

Swap two values

Swap the two values so the program prints right left. Hint: a third variable can hold one value for a moment.

Expected output
right left
Your code
let a = "left";
let b = "right";
// Swap the values of a and b here.

console.log(a, b);
Hint

a = b; first loses the old a. Keep it in a third variable before you overwrite it.

A solution
let a = "left";
let b = "right";
const t = a;
a = b;
b = t;
console.log(a, b);

11Explain

Common mistakes

const max = 5;
max = 6;
Error message
TypeError: Assignment to constant variable. (line 2)
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

const means the name keeps its value. Use let for a value that will change.

console.log(total);
let total = 3;
Error message
ReferenceError: Cannot access 'total' before initialization (line 1)
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

A let or const variable cannot be used above the line that declares it.

12

Summary

  • let name = value; declares a variable that can change.
  • const name = value; declares one that cannot be reassigned.
  • = means store, not equals.
  • x = x + 1 reads x, adds 1, and stores the result back.
  • Names: letters, digits, _ and $, no leading digit, case sensitive, camelCase.

Next unitNumbers and math
Full text · The whole lesson as one text

Naming a value so you can use it again

So far every value has been typed straight into console.log and then forgotten. Real programs need to remember things: the player's score, the items in a basket, the name someone typed in. A variable is a named box that holds a value. You create it once, then use its name wherever you need the value.

let score = 10;
console.log(score);        // 10
console.log(score + 5);    // 15

The line let score = 10; does two things. The keyword let declares a new variable called score, and = 10 puts the value 10 into it. The equals sign here does not mean "is equal to" as in maths. It means "store the value on the right in the name on the left." Programmers call this assignment. When score appears later without quotes, JavaScript looks up the box and uses the value inside.

Changing a value with let

A variable declared with let can be given a new value later. You write the name, an equals sign and the new value, without let this time, because the variable already exists:

let score = 10;
score = 25;
console.log(score);        // 25
score = score + 5;
console.log(score);        // 30

The line score = score + 5; looks impossible as maths, but read it as an instruction and it makes sense. JavaScript first evaluates the right side, the current score plus 5, which is 30. Then it stores 30 back into score. The old value is gone. This pattern, reading a variable and storing an updated value into it, is how programs count, add up totals and keep track of anything that changes.

const for values that stay put

The keyword const also declares a variable, but it must be given a value straight away and can never be assigned a new one. Trying to do so stops the program with TypeError: Assignment to constant variable. That error is useful. If a value is not supposed to change, such as the number of days in a week or a tax rate, declare it with const. Then a mistake elsewhere in the program cannot quietly overwrite it.

const daysInWeek = 7;
console.log(daysInWeek * 4);   // 28
// daysInWeek = 8;  would stop with a TypeError

A good habit, recommended by many style guides, is to use const by default and switch to let only when you know the value will change. You may also see the older keyword var in code on the web. It still works, but it follows looser rules that cause surprising bugs, so this course uses let and const only.

Rules for names

A variable name may contain letters, digits, the underscore _ and the dollar sign $, but it cannot start with a digit and cannot contain spaces. So total2 is fine and 2total is not. Names are case sensitive: score and Score are two different variables. A few words such as let, const, if and for are reserved by the language and cannot be used as names. When a name is made of several words, JavaScript programmers usually write it in camelCase: the first word in lower case and each later word starting with a capital, as in totalPrice or daysInWeek. Pick names that say what the value means; price tells a reader far more than x.

Declared but empty, and used too early

If you write let answer; with no value, the variable exists but holds undefined until you assign something. Using a let or const variable on a line above its declaration is an error: ReferenceError: Cannot access 'answer' before initialization. Using a name that was never declared at all gives ReferenceError: answer is not defined. Both messages mean the same practical thing: check the spelling and check that the declaration comes first.

let name = value; can change later. const name = value; never changes.

Key points
  • let name = value; declares a variable that can change.
  • const name = value; declares one that cannot be reassigned.
  • = means store, not equals.
  • x = x + 1 reads x, adds 1, and stores the result back.
  • Names: letters, digits, _ and $, no leading digit, case sensitive, camelCase.

Practice questions from Variables: let and const on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (2)
  • MDN Web Docs, JavaScript Guide (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, chapters Introduction, Grammar and types, Control flow, Loops and iteration, Functions, Expressions and operators, Numbers and strings, Indexed collections, Working with objects; checked 2026-10-17)
  • Ecma International, ECMA-262 ECMAScript 2026 Language Specification, 17th edition, June 2026 (tc39.es/ecma262 and ecma-international.org/publications-and-standards/standards/ecma-262, checked 2026-10-17).

Checked: 2026-10-17