Locked lesson.
About this lesson
How to add, subtract, multiply, and divide with JavaScript. We'll also look at the modulus, incrementing and decrementing.
Exercise files
Download this lesson’s related exercise files.
Arithmetic Operators.docx58.7 KB Arithmetic Operators - Solution.docx
58.9 KB
Quick reference
Arithmetic Operators
Math operators are very useful to computer programming.
When to use
Any time you want to do any sort of math, you'll use arithmetic operators.
Instructions
The main math operators are:
+ // addition
- // subtraction
* // multiplication
/ // division
** // exponents
% // modulus
++ // increment
-- // decrement
Hints & tips
- Math operators act exactly how you'd expect them to.
- Remember to use the correct order of operations!
- 00:04 In this video I wanna talk about arithmetic operators, math operators.
- 00:09 So we've talked a little bit about math in the last video but
- 00:12 there's a bit more to talk about.
- 00:13 So here are the main math operators, and
- 00:16 they're exactly like you'd think they would be.
- 00:18 Let's go ahead and delete this.
- 00:20 We've got addition, subtraction, multiplication,
- 00:23 division, exponents, and then we've got the modulus.
- 00:27 We talked about that a little bit in the last video.
- 00:29 And then we have these two that you may not have heard of, increment and
- 00:32 decrement.
- 00:33 So these all act, like I said, exactly like you'd think.
- 00:37 This adds things.
- 00:38 This subtracts things, multiplies, divides.
- 00:40 This does exponents.
- 00:41 But these last three may not be familiar to you.
- 00:43 We kind of talked about modulus already in the last video.
- 00:46 I'll run through these very quickly and then we'll talk about these last three.
- 00:49 So, lets do a little bit of math here.
- 00:50 We've got our script tags.
- 00:52 We can do just very basic math, go to document.right,
- 00:57 and inside here we can go 2 + 2 and that,
- 01:01 hit reload, boom, we got 4, 2- 2 is 0.
- 01:05 We could go 2 exponent 2, 2 to the second power, which is 4.
- 01:11 Or 2 to the 8th power which is 256.
- 01:17 We can do division, all the good stuff.
- 01:21 Now we can also assign variable names.
- 01:23 We can go var x equals 1, let's say 2.
- 01:28 And var y equals 3.
- 01:31 And in here we can go x plus y.
- 01:35 So you don't have to actually use numbers, you can use variables too.
- 01:37 So 2 plus 3, that's gonna be 5 and all that good stuff.
- 01:41 So, that's very basic.
- 01:42 Next, let's talk about the increment and the decrement.
- 01:45 I'm not even sure with decrement is a word, it sounds a little funny.
- 01:48 But let's go ahead and, let's go var x = 10.
- 01:54 And x is a horrible variable name but I'm just using it 'cause it's quick and easy.
- 01:58 Now we can go x++, that's increment.
- 02:02 And now if we just write out x, what do you think the answer's gonna be?
- 02:06 So we're taking 10, we're incrementing it by 1.
- 02:09 So it should be 11.
- 02:10 Same thing, we can do decrement to decrease it and it's gonna be 9.
- 02:17 So, this is actually very useful.
- 02:19 You'll use this all the time for a bunch of different things.
- 02:21 And so those are your basic math operators.
- 02:24 Now I wanna spend just a minute talking about order of operations.
- 02:27 Now this is something you talk about in grade school math and
- 02:29 you probably forgot about it.
- 02:30 But order of operations is important.
- 02:32 So if we go var x equals 8 plus 2 times 3 and
- 02:38 then we write out x, what's the answer gonna be?
- 02:45 Is it gonna be 8 plus 2, which is 10 times 3 which is 30?
- 02:48 Or is it gonna be 2 times 3 which is 6 plus 8 which is, what, 14?
- 02:53 Let's see, the answer is 14.
- 02:56 Because in the math order of operations,
- 02:59 multiplication takes precedence over addition.
- 03:02 So you always do multiplication first.
- 03:04 Likewise, you do division before you do addition or subtraction.
- 03:07 So you may have to sort of brush up on your order of precedence,
- 03:10 order of operations.
- 03:11 You could look up on Google if you need to remember that.
- 03:13 You can change this, you don't have to use the order operations,
- 03:16 you can sort of over ride this by using parenthesis.
- 03:20 So whatever is inside the parenthesis gets done first,
- 03:23 that's also an order of operations rule.
- 03:25 So now the answer's gonna be 10 x 3, we save this should be 30, and it is.
- 03:31 So that is math.
- 03:32 Math is very simple, but you are going to use it a lot whenever you use JavaScript.
- 03:35 So that's math and the next video we'll look at comparison and logic operators.
Lesson notes are only available for subscribers.