Changing Values
Welcome to the next step in your programming journey! You already know how to create variables to store information. But apps and games are rarely frozen in time. Things happen, and your stored information needs to update.
Why values change
Think about playing your favorite video game.
- You start with a score of zero.
- You collect a coin, and your score goes up.
- You take damage, and you lose a life.
In programming, we need our variables to update dynamically as these events happen. We need containers that can swap out their old contents for new ones.
Changing a value with let
To create a variable that can change, we make it using the let keyword. The word let signals to JavaScript that this value is allowed to update later on.
(Note: If you have a value that should stay exactly the same forever, you can use the const keyword instead. Use let for values that need to change, such as scores and counters.)
Once a variable is created with let, you can give it a brand new value by simply using the equals sign (=). You do not need to type let a second time.
First code example
Let's look at a simple game score that increases over time.
let score = 0;
score = score + 10;
console.log(score);
score = score + 5;
console.log(score);Step-by-step explanation
Let's break down exactly what is happening in the code above:
-
let score = 0;We create a new variable namedscoreand set its starting value to0. -
score = score + 10;JavaScript always looks at the right side of the equals sign first. It takes the currentscore(which is0), adds10to it, and stores this new result back into thescorevariable. -
console.log(score);We send the score to the Output section to see the update. The first output will be10. -
score = score + 5;Again, it calculates the right side first. It takes the currentscore(which is now10), adds5, and stores the final result (15) back into the variable.
Mini task
Look at the code below. Without running it, try to guess what final number will appear in the Output section.
let lives = 3;
lives = lives - 1;
lives = 5;
console.log(lives);Hint: Variables only remember their most recent value!
Then click Run and check the Output section to see if your guess was correct.
Short quiz
1. Which keyword should you use to create a variable if you know its value will need to change later?
A) const
B) let
2. How do you give a new value to a variable named counter that has already been created?
A) let counter = 5;
B) counter = 5;
(Answers: 1. B, 2. B)
Small challenge
Now it is your turn to practice!
- Create a variable named
pointsusingletand set it to10. - Change the value of
pointsby adding20to its current value. - Use
console.log(points)to show the result.
Click Run and check the Output section below the code to see if it works!
// put your code below
console.log()Summary
- Variables created with
letare flexible and can change over time. - You update a variable by using its name and the equals sign (
=). You only use the wordletthe very first time you create it. - You can use a variable's current value to calculate its new value, like
score = score + 10. - If a value should never change, use
constinstead.