Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Once you declare a variable and assign it a value, your program can manipulate what's stored in the variable as it runs, like a score in a game, for example.
Resources
- Addition (
+
) – MDN - [Addition assignment operator – MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition_assignment
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You've learned how to declare name and
assign information or
0:00
values to variables, as well as how
to assign a new value to a variable.
0:03
Remember, variables hold
values that can change.
0:08
Once you declare a variable and assign
it a value, your program can manipulate
0:11
what's stored in the variable as it runs,
like the score in a game for example.
0:15
You can use variables to do
a variety of different things
0:20
with the value stored in or
assigned to them.
0:23
Remember the variable score,
it holds a number value, let's say zero.
0:26
Earlier you learn that you can
change what's inside a variable
0:31
through reassignment like this,
score = 10.
0:36
Logging score to the console shows
that the value of score is now 10.
0:40
Well, we can change the value of
a variable like score, we are overriding
0:46
the value every time, rather than
keeping a running total of the score.
0:52
Think about if you were
programming a game and
0:57
wanted to keep track of a player
score using the variable score.
0:59
Well instead of overriding the value of
score each time a player scores points,
1:02
you'd most likely want to keep
adding to the total score.
1:07
In other words, instead of completely
replacing what's inside the labelled
1:10
box with something else, we want to add
to the current contents of the box.
1:14
One way to add to the score is like this,
score = score + 10.
1:19
This might look a bit strange.
1:26
The variable score appears twice in
the statement, so how does this work?
1:28
Remember, when putting
a value into a variable,
1:33
whatever's to the right of the equal
sign goes into the variable on the left.
1:37
In this case,
1:43
what's on the right is the current
contents of the variable score + 10.
1:44
The sum of the two values is placed
back into the variable score.
1:49
Since score currently equals 0,
it's just like saying score = 0 + 10.
1:54
The idea of accessing a variable to update
a variable still might seem a bit strange,
2:02
but it's really common in programming to
update a variable based on the value it
2:06
already has in it.
2:10
In fact, it's so
2:12
common that there's a shorthand method for
adding to the contents of a variable.
2:13
Replace this part here with a + sign and
an = sign.
2:17
This is called an addition
assignment operator, and
2:23
you'll see it a lot in JavaScript.
2:27
To increase the score by a certain amount,
2:29
you can use the addition assignment
operator to add to its current value.
2:32
For instance, score += 5.
2:37
Since the value of score was 10,
I added 10 + 5, and now score is 15.
2:42
You can also add variables
representing number values together.
2:52
For example, declare a new variable named
2:55
bonusPts and assign it the value 100.
3:01
I can add the current score value
with the value of bonusPts like this,
3:06
var finalScore = score + bonusPts.
3:14
We are adding the current value of
the variable score plus the value
3:18
of the variable bonusPts and saving the
sum of those values to a third variable,
3:22
finalScore.
3:27
Log finalScore to the console,
and the result is 115.
3:31
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up