Loading video player…

Update a Value From Its Current Value

In this lesson, you kept score for a board game in a script, score.js, and updated a name from its own current value.

Key Points

  • JavaScript works through an update in two steps. First it works out the right side, using the value the name points at right now. Only then does the name on the left move to the result.
  • You create the name once with let. The update lines have no let, because the name already exists and you’re moving it, not creating it again.
  • A script runs from top to bottom, so each update starts from wherever the name points at that moment. Starting at 20, adding 10 and then 5 gives 30, then 35. That’s a running total.
  • A calculation on its own, like score + 10 with nothing in front of it, changes nothing. JavaScript works out 30 and throws it away, and score still points at 20. To keep a result, point a name at it.

The Final Code

Language: JavaScript Filename: score.js
let score = 20;
score = score + 10;
console.log(score);
score = score + 5;
console.log(score);
Language: Shell
$ node score.js
30
35

The experiment with a calculation on its own:

Language: JavaScript Filename: experiment.js
let score = 20;
score + 10;
console.log(score);
Language: Shell
$ node experiment.js
20

Next up is How On-Site Exercises Check Your Code.

00:00 In the last lesson, you used let to give your values names. Now it’s game night, and you’re the one keeping score for a board game. Your score starts at 20 points.

00:11 So the first line of your script creates the name score with let and points it at 20.

00:18 Then you land a lucky move worth 10 more points. You could do the sum in your head and point score at 30. However, mental math on game night is how scoresheets go wrong.

00:28 What you want is a line that says: whatever score is right now, plus 10. Here’s that line. It starts with score and an equals sign, and score + 10 sits on the right.

00:42 As in the last lesson, read the equals sign as “now points at.”

00:48 JavaScript works through this line in two steps. First, it works out the right side. It looks up score, which points at 20 right now. It adds 10 and gets 30.

01:03 Only then does the left side happen. The name score lets go of 20 and moves to 30. Notice that there’s no let on this line. The name already exists, so you’re moving it, not creating it again.

01:18 Add a line that logs score, save the file, and give it a spin in the terminal. It prints 30.

01:27 Next round, you score 5 more. Add the same kind of line again with 5 this time, and log score once more. JavaScript runs your script again, from top to bottom.

01:39 When it reaches the new line, score already points at 30, so the right side works out to 35. Then the name moves to 35.

01:49 Run the script again, and you get 30, then 35. Each update starts from wherever the name points right now. That’s a running total: one name, created once, that keeps moving as the game goes on.

02:06 Now for a small experiment, in a fresh file. It starts score at 20 again. The middle line has score + 10, with nothing in front of it. The last line logs score to the terminal.

02:18 Your turn: pause the video and predict what this script prints.

02:27 It prints 20. JavaScript did work out 30. But nothing on that line told a name to move, so the 30 was thrown away, and score stayed at 20. A calculation on its own changes nothing.

02:42 To keep a result, point a name at it.

02:47 So here’s the rule for updating a value from its current value. The right side comes first, and then the name moves.

02:55 You created score once with let, and each update started from its current value, line by line, top to bottom. A calculation with no name to receive it leaves everything as it was.

03:08 Next up is How On-Site Exercises Check Your Code, where you’ll see how the exercises here check what your code hands back. Until then, happy scorekeeping.

Become a Member to join the conversation.