Locked lesson.
About this lesson
How to assign values to a variable, and do basic math assignment.
Exercise files
Download this lesson’s related exercise files.
Assignment Operators .docx58.7 KB Assignment Operators - Solution.docx
58.8 KB
Quick reference
Assignment Operators
Assignment operators are used to "assign" things to other things.
When to use
Anytime you want to assign something to something else, you'll use an assignment operator.
Instructions
The most common assignment operator is the equal to sign =
Other common assignment operators are:
=
+=
-=
*=
/=
%=
Hints & tips
- You'll mostly use the equal to sign to assign
- You can use math assignment operators to shortcut your math work
- 00:05 In this video I wanna talk about assignment operators.
- 00:07 Now assignment operators assign things to other things.
- 00:10 And we've actually already seen this when we talked about variables.
- 00:13 This var first name = John.
- 00:17 We are assigning John to the variable first name.
- 00:20 And we do it with this equal to sign.
- 00:22 So this equal to sign is an assignment operator.
- 00:25 Right, it assigns something.
- 00:27 The equals to sign is probably the most common assignment operator,
- 00:31 you're gonna see that all the time.
- 00:32 But there are a whole bunch of other ones, too, and
- 00:34 we're gonna talk about those in this video.
- 00:35 So what are some of those other ones?
- 00:41 Well you've got +=, we've got -=, we've got *=, we've got /=, we've got modulus =
- 00:45 and that's pretty much all the ones we're going to talk about in this video.
- 00:49 There are some other ones that are more esoteric and
- 00:51 you won't see very often but these are the main ones.
- 00:53 These do pretty much what you would expect them to do.
- 00:56 They do some math and they assign the answer to something else.
- 00:59 So like as you would expect this one adds an equals to.
- 01:02 This one subtracts an equals to, this one multiplies an equals to,
- 01:05 this one divides an equals to, and this one does a modulus equal to.
- 01:09 You may not be familiar with that one.
- 01:10 It's not one people are often familiar with.
- 01:12 But a modulus just finds a remainder.
- 01:14 Remember when you divided when you were like in fourth grade in grade school.
- 01:19 For instance, three goes into eight two times, with two remainder, right?
- 01:24 So that's what this modulus does it finds the remainder.
- 01:27 Like I said, we'll talk about math in a couple of videos from now.
- 01:30 But, for right now, let's just kinda look at this, and let's erase all this.
- 01:34 And let's go var myNumber =,
- 01:40 I don't know, 20, right?
- 01:44 So we use one of these assignment operators to go myNumber += 5, right?
- 01:51 So what do we think this is gonna do?
- 01:53 Well, we can do a document.write and then write out my number to the screen.
- 02:00 Save this and let's take a look at this.
- 02:03 And boom, the answer is 25.
- 02:04 So what it's doing is it's taking 20 and it's adding five to it and
- 02:08 then slapping the answer back into the variable.
- 02:10 So it would be sort of like if you went myNumber = myNumber + 5, right?
- 02:21 If we save this, come back and hit reload, we're gonna get the same answer 25.
- 02:25 But this is just a much shorter, easier, more elegant way to do that.
- 02:32 Right, basically that's all it is.
- 02:34 And it's the same thing with all the rest of these,
- 02:36 if we go -= 2 that would be 20- 5 so our answer is going to be 15.
- 02:41 If we go *= it's going to be, what, 100?
- 02:46 If we go /= it's going to be 4, and
- 02:50 if we go modulus = it's probably going to be 0, right?
- 02:54 Right, so we can change this around and go 11, right?
- 02:57 5 goes into 11 two times, with 1 left over.
- 03:00 'Cause 2 times 5 is 10.
- 03:02 11- 10 is 1.
- 03:03 So if we save this, it should be one.
- 03:05 Yup, there we go.
- 03:06 So assignment operators, very useful, we'll use these a lot for
- 03:10 a lot of different things and like I said, the equal to sign is probably going to be
- 03:13 the most common one that you're going to use.
- 03:14 You're always going to be assigning things to variables and
- 03:17 all kinds of other things, so it's just second nature.
- 03:20 But you'll actually use those other ones as well so
- 03:22 just sort of keep those in mind.
- 03:24 So that's all for
- 03:24 assignment operators and in the next video we'll talk about math operators.
Lesson notes are only available for subscribers.