3.2 Arithmétic Operators
Hello everyone and welcome to this video so we're going to see the 1st category of operator that we're going to look at which is the arithmetic operators. We use it to perform calculations just like calculations in mathematics. Let's look at all the arithmetic operators in JavaScript. We have here two variables x and y, so we will make a console.log Here is the first example x + y which is the addition operator. So arithmetic operators usually take two ranger and then produce a new value, what we have here is x + y is what we call an expression in JavaScript. So an expression is something that produces a value. So here is the addition operation, we also have a subtraction. A multiplication A division The remainder of the division. So, just like the basic arithmetic operators in math, we have a new operator in javascript which is exponentiation. It is indicated by two stars that is x ^ y , now we will see two types of arithmetic operators that are fundamental in JavaScript. For now I will comment on these few lines, so we select this piece of code and press ctrl + / so these two operators are increment and decrement operators. Let's see how it works. The increment is indicated by two + signs and depending on where we put the + signs, this operator will behave differently. So I'm going to make a console.log of x. X is initialized to 10, if I save the changes, we see 10 on the console. Now if I put the increment operator before the x, the value of x will be incremented by 1 first, then we'll see that on the console. Let's have a look to save the changes, we get 11. On the other hand if you put this operator after the x we will see x on the console first and then the value of x will be incremented by 1. We save the changes and we get 10, but at this point x is incremented by 1 so if we do another console of x, now we should see 11, so the value of x is incremented by 1. The decrement operator is quite similar. Instead of two + signs, we have two - signs, here I'm going to apply that before the x, so we're going to decrement the value of x by 1, and then display it on the console. So for this demo I'm going to comment on these two lines. Let's take a look, we see 9 on the console because we apply this operator first and then display x on the console. So here are the arithmetic operators that we have in JavaScript, to recap we have addition, subtraction, multiplication, division the remainder of the division, the exponentiation which is x ^ y, as well as the increment and decrement operators. That's it for this video on operators in JavaScript, we'll see you on the next video where we'll talk about assignment operators.