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 questionsYou 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.
let score = 10;console.log(score); // 10console.log(score + 5); // 15
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.
let score = 10;score = 25;console.log(score); // 25score = score + 5;console.log(score); // 30
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.
let a = 5;
let b = a;
a = 9;
console.log(a, b);9 55Problem
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 TypeError287Problem
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.
15let 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.
right leftlet 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;TypeError: Assignment to constant variable. (line 2)
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- 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;ReferenceError: Cannot access 'total' before initialization (line 1)
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- 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.