1 00:00:00,485 --> 00:00:03,910 You've learned how to declare name and assign information or 2 00:00:03,910 --> 00:00:08,400 values to variables, as well as how to assign a new value to a variable. 3 00:00:08,400 --> 00:00:11,510 Remember, variables hold values that can change. 4 00:00:11,510 --> 00:00:15,440 Once you declare a variable and assign it a value, your program can manipulate 5 00:00:15,440 --> 00:00:19,020 what's stored in the variable as it runs, like the score in a game for example. 6 00:00:20,460 --> 00:00:23,250 You can use variables to do a variety of different things 7 00:00:23,250 --> 00:00:26,590 with the value stored in or assigned to them. 8 00:00:26,590 --> 00:00:30,680 Remember the variable score, it holds a number value, let's say zero. 9 00:00:31,710 --> 00:00:36,347 Earlier you learn that you can change what's inside a variable 10 00:00:36,347 --> 00:00:40,032 through reassignment like this, score = 10. 11 00:00:40,032 --> 00:00:46,840 Logging score to the console shows that the value of score is now 10. 12 00:00:46,840 --> 00:00:52,850 Well, we can change the value of a variable like score, we are overriding 13 00:00:52,850 --> 00:00:57,520 the value every time, rather than keeping a running total of the score. 14 00:00:57,520 --> 00:00:59,560 Think about if you were programming a game and 15 00:00:59,560 --> 00:01:02,670 wanted to keep track of a player score using the variable score. 16 00:01:02,670 --> 00:01:07,310 Well instead of overriding the value of score each time a player scores points, 17 00:01:07,310 --> 00:01:10,110 you'd most likely want to keep adding to the total score. 18 00:01:10,110 --> 00:01:14,500 In other words, instead of completely replacing what's inside the labelled 19 00:01:14,500 --> 00:01:19,240 box with something else, we want to add to the current contents of the box. 20 00:01:19,240 --> 00:01:26,233 One way to add to the score is like this, score = score + 10. 21 00:01:26,233 --> 00:01:28,480 This might look a bit strange. 22 00:01:28,480 --> 00:01:33,590 The variable score appears twice in the statement, so how does this work? 23 00:01:33,590 --> 00:01:37,330 Remember, when putting a value into a variable, 24 00:01:37,330 --> 00:01:43,460 whatever's to the right of the equal sign goes into the variable on the left. 25 00:01:43,460 --> 00:01:44,446 In this case, 26 00:01:44,446 --> 00:01:49,639 what's on the right is the current contents of the variable score + 10. 27 00:01:49,639 --> 00:01:53,010 The sum of the two values is placed back into the variable score. 28 00:01:54,320 --> 00:02:01,040 Since score currently equals 0, it's just like saying score = 0 + 10. 29 00:02:02,260 --> 00:02:06,840 The idea of accessing a variable to update a variable still might seem a bit strange, 30 00:02:06,840 --> 00:02:10,980 but it's really common in programming to update a variable based on the value it 31 00:02:10,980 --> 00:02:12,240 already has in it. 32 00:02:12,240 --> 00:02:13,130 In fact, it's so 33 00:02:13,130 --> 00:02:17,830 common that there's a shorthand method for adding to the contents of a variable. 34 00:02:17,830 --> 00:02:23,930 Replace this part here with a + sign and an = sign. 35 00:02:23,930 --> 00:02:27,050 This is called an addition assignment operator, and 36 00:02:27,050 --> 00:02:28,660 you'll see it a lot in JavaScript. 37 00:02:29,760 --> 00:02:32,790 To increase the score by a certain amount, 38 00:02:32,790 --> 00:02:37,020 you can use the addition assignment operator to add to its current value. 39 00:02:37,020 --> 00:02:42,461 For instance, score += 5. 40 00:02:42,461 --> 00:02:49,871 Since the value of score was 10, I added 10 + 5, and now score is 15. 41 00:02:52,252 --> 00:02:55,970 You can also add variables representing number values together. 42 00:02:55,970 --> 00:03:01,064 For example, declare a new variable named 43 00:03:01,064 --> 00:03:06,024 bonusPts and assign it the value 100. 44 00:03:06,024 --> 00:03:14,171 I can add the current score value with the value of bonusPts like this, 45 00:03:14,171 --> 00:03:18,610 var finalScore = score + bonusPts. 46 00:03:18,610 --> 00:03:22,722 We are adding the current value of the variable score plus the value 47 00:03:22,722 --> 00:03:27,643 of the variable bonusPts and saving the sum of those values to a third variable, 48 00:03:27,643 --> 00:03:28,540 finalScore. 49 00:03:31,046 --> 00:03:36,891 Log finalScore to the console, and the result is 115.