Course Content

  • 2.4 arithmetic_operator

Course Content

JavaScript arithmetic operators


Java script arithmetic operators


Hello guys, welcome to our series Java script. In our last segment we saw about data types in Java script. There are two types of data types, primitive data type and non-primitive data type. In primitives we saw numbers, string, Boolean etc. and in non primitive we saw data types like array and object. I hope you all have practiced everything we did in last segment by performing practically.

If you have any questions or queries in our last segment please do tell us on we will revert back with solutions to all your question and queries. Moving further in our today’s segment, our today’s segment is about operators in Java script.

In operators in Java script we are going to learn about all the operators which work on operand. Basically operator means, let’s say we have two number X and Y, so whatever operations we perform between these two numbers we do it through operators. Let’s say I have to perform operation with 2 +2 , so here 2 and another becomes our operands and the plus sign between them becomes my operator.

So 2+2 which is 4, so this summation is performed by operator. If I want to do summation then I will use plus operator and if I want to perform subtraction then I will use minus operator. So these different operators, in our Java script how many different operators are available we will study in our today’s segment.

We have in total 6 types of operators available in Java script. These six operators are used to perform different types of operations. Basically what is operator? So the definition is something like this, Java script operators are symbols that are used to perform operations on operands. Means such symbols with help of which operations are performed on operands, are known as operators.

All these operators mentioned here, we are going to study one by one in today’s segment. We will start with our first operator i.e. Arithmetic Operators. So what is arithmetic operator? Things which we are learning from childhood through mathematics, all those are arithmetic operators. If you have learned any other programming language before Java script, for example C or Java, basically we all have studied C in college or school.

Even in C these same operators are used, but in this segment we will study it freshly, each and every operator we will see in depth. So basically what are arithmetic operators? Arithmetic operators are used to perform arithmetic operations. Means all the operations related to mathematics for that the operators used are our arithmetic operators.

Here you can see plus, minus, multiplication, division, these four are our common arithmetic operators. How to use these in Java script we will see through a program. So let’s say opening sublime text, in sublime text we will store it in html , html example arithmetic operators, save (5 seconds gap) so here arithmetic and this our program got stored.

Now in arithmetic what we have to do? Script, because we are doing this for Java script, so in script we are going to perform all the operators shown over here. So let’s say here some examples are also given like 1+1, 1-1 etc. so let’s say I take a variable in this script, var x= 10 and var y= 20. Now I will perform all my operations on this x and y, so the first operation is addition. So for addition we will directly write document.write summation, colon and I will give some space and here I am using plus sign.

Here we have to understand some differences also such as plus sign has different uses also apart from arithmetic operations for example if we are showing some string in output, as I have written here document.write, so here you can see my summation is inside quotes so to join a string a have used plus sign outside the quote. Whenever for writing purpose I add plus sign along with quotes or string then it will convert the next string which comes or whatever comes next and print it as string.


For example if I write here summation and subtraction, we are writing this for example, save, this is our arithmetic , okay, so here you can see summation and subtraction, so here what happened? The two strings which I wrote in program I append them with plus sign. So whatever is written after plus, you will get that as string here. Let’s say if I remove this and write 50 which is a number, I will save and reload it, so here after colon 50 will appear.

Whatever we write after colon will be converted as string irrespective of it being a number or Boolean. But I want to show the summation of these two here, so for that what will I do? After plus in bracket I will write x+y, now let’s see what answer we will get? So we are getting 30. Means in x we have 10 and in y we have 20 so summation of x and y we get as 30.

Now if I remove this bracket and I write, reload , now you can see the value of x and y is getting printed, summation 1020 ie. 10 is x and 20 is y, but don’t want to show this. Here bracket has lot of importance, the importance of parenthesis which we used is, whatever I am writing after plus that will be converted as string and shown. That is because left to right rule is followed while execution.

So first summation and then the string comes on left side and then x and y, i.e. =whatever our values are. So if I want this to be considered in the form of mathematics, i.e. I want to perform arithmetic operation on this so I will shift it and add bracket, as soon as I add parenthesis the value changes to 30, that is my operation is getting performed.

Guys what you have to remember here is , plus sign , because in Java script plus sign is used for appending strings, means to join two strings. So whenever you use plus sign keep in mind that if x and y are used, that x and y are our variables, that is true, but if you have to perform arithmetic operation or any other operation then you make sure you write it in parenthesis, otherwise that data will be considered as string.

If I write the same thing, here I will add a br tag (4 seconds gap) so summation, now comes subtraction i.e. our minus so summation comes as 30 and subtraction comes as -10, because 10-20, that is we are subtracting higher value from lower value so our answer will be in negative.

Okay , so here subtraction, here comes our multiplication, we will replace it with star, here will come division so we will replace it with modulus, save and reload. So here comes 30, -10,our multiplication of 10 and 20 i.e. 200, division comes as 0.5. so these were our arithmetic operations, we will add one more line here, (24 seconds gap) okay so tour arithmetic operators are appearing here .


If we move to our slide, we still have three more operations, one is modulo operator, so what is the use of modulo operator? Whenever we perform division we get remainder with the help of modulo operator. So let’s say I store 3 in variable z, so if I perform division, here comes remainder of 10 and 3 is, so here will come x modulo operator and then z will come. Value of x is 10 and value of z is 3, so if divide 10 by 3 then I will get 1 as remainder, I will save this and reload, so my answer is 1.

Remainder of 10 divided by 3 is 1, so our answer is remainder which we get with the help of modulo operator. When do we use modulo operator? Whenever we want the remainder from a division instead of answer we use modulo operator.

After remainder comes increment operator, increment operator is written as ++i. increment operator are of two types, if we write ++ before then it pre and if we write ++ after variable then it is post. First we will see what does increment operator do? Increment operator increases by 1 in our current variable value, for example if value of x is 10 and if I perform increment operator then the value increases from 10 to 11.

Again if I am performing increment value then value increases from 11 to 12. The increase of 1 is because of increment operator. If I use decrement operator, then value of x which is 10 will get decreased by 1, the value will become 9. Again if I am using decrement operator then value will change to 8.

If I have to increase the count by 1 then I will use increment operator and if I want to decrease the count by 1 then for that I will use decrement operator. So let’s say I have to make increment in x then I will write here, document.write “Pre increment on x:” plus , pre means prior si it will be ++x, let’s save it and reload it, so you can see I am getting 11 as value. It means plus plus, you can see here I have written plus plus before x, so if I am writing plus plus prior to variable, then the value of x before printing, i.e. before execution of this line the value of x increase by 1.

But if I write plus plus after the variable then the execution won’t take place in current line, execution will be performed from next line onwards. So let’s say if I write post increment on x and I write plus plus after x, save and reload, so you can see post increment on x is 10, so I have written x and after that I have written plus plus so I am getting 10 only as value.

But if I print value of x again, document.write(4 seconds gap) okay, so you can see value of x is 11. Here we are getting 11, means in this statement, in line number 27, I have not performed any increment operation but I have performed increment operation on line number 25. So if I write plus plus after variable, then where the x line is written, means the line of code is written, there increment will not be performed, operation will be performed in the line after that, after the execution of statement the value of x will increase.

So if I want to increase it prior then I will write plus plus prior to variable, and if I want to increase it later then I will write the plus plus later. If we write it properly, it should be like pre increment on x i.e. plus plus, okay, br, post increment on x, i.e. plus plus x will change 10 to 11 but in this line 11 only will get printed, because we are doing increment after the execution of line. Okay, so pre increment is also 11 and post increment is also 11, if use value of x after line number 27, then I will get 12 because I have performed post increment here.

So guys make sure wherever we use pre, our value will be used after adding 1. But if we are using post then the increment will be performed after execution of the line. So here in line 25 while executing and here in line 27 after executing. If I perform decrement operator so here comes pre decrement on x and here post decrement on x. so here minus minus x and here x minus minus, So once we will deduct value prior and once we will deduct value later. So save and reload, here again I am getting 11, because in line 27 the value of x is 12 so if I am performing pre decrement the the vakue will be decreased by 1 and I will get 11.

Now I am again performing post decrement , so here value will be printed 11, value will be decreased in the later line. Again my value of x is 10.( 8 sedonds gap) as you can see we are getting 10 here. Okay so value of x is 10. We will comment this line.

So these were our increment and decrement operators. So all these operators which we saw are our arithmetic operators. Guys what we learned today, we are going to continue the same in our next segment. Keep practicing , good bye.

(video duration- 17 minutes 39 seconds)

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere