programminghead Logo

JavaScript Tutorial 5 : JavaScript Arithmetic Operations

Arithmetic Operations :

Arithmetic Operators takes numeric value as their Operands and returns a single value in results.

Operand Operator Operand Result
2 + 5 = 7

Standard Arithmetic Operators :

There are Four standard Arithmetic Operators :

1. Addition Operator (+),
2. Subtraction Operator (-),
3. Multiplication Operator (*) and
4. Division Operator (/).

Addition Operator (+) :-

Addition Operator add two or more numeric Operands and gave Total value in Result.
LIKE 2+3=5
In JavaScript we can create Variables to store our Numeric Value (Operands) and then use those value by Calling JavaScript Variables or we can directly use our Numeric Operands and Arithmetic Operators in the same line without using JavaScript Variables.

Addition Operation (without using JS Variables) :


   document.write(2+6);

Results :

Addition Operation (using JS Variables) :


   var a,b,c;
   a=2;
   b=6;
   c=a+b;
   document.write(c);

Results :


Subtraction Operator (-) :

Subtraction Operators can subtract two or more Numeric Operands and gave results in Numeric Data.
Like : 5-2=3
Here 5 and 2 is Numeric Operands and - is JavaScript Subtraction Operator and 3 is our Result.

Subtraction Operation (without using JS Variables) :


   document.write(7-2);

Results :

Subtraction Operation (using JS Variables) :


   var a,b,c;
   a=7;
   b=2;
   c=a+b;
   document.write(c);

Results :


3. Multiplication Operator (*) :

Multiplication Operator Multiply the two or more Numeric Operands and gave us result in numeric Value.
Like : 3*2=6
Here 3 and 2 is our Operands and * is JavaScript Multiplication Operator and 6 is Result of two Operands (3 and 2).

Multiplication Operation (without using JS Variables) :


   document.write(7*2);

Results :

Multiplication Operation (using JS Variables) :


   var a,b,c;
   a=7;
   b=2;
   c=a*b;
   document.write(c);

Results :

Division Operator (/) :

Division Operator Divide the two or more Numeric Operands and gave us result in numeric Value.
Like : 6/2=3
Here 6 and 2 is our Operands and / is JavaScript Division Operator and 3 is Result of two Operand's Division (3 and 2).

Division Operation (without using JS Variables) :


   document.write(6/2);

Results :

Division Operation (using JS Variables) :


   var a,b,c;
   a=6;
   b=2;
   c=a/b;
   document.write(c);

Results :


Remainder Operator (%) :

Remainder Operator returns the reminder value from two Operand values Division.
7%2=1
Here 7 and 2 is our Operand Values and % is our Remainder Operator and 1 is our value remained after the Division.

Example


   document.write(9%2);

Results


Exponentiation Operator (**) :

Exponentiation Operator retures the Value of two or more Operands where 2nd Operand become the power of first Operand.
LIKE : 2**3=8
explained : 23=8 (here 23 means we have to multiply 2 tree times like 2*2*2)
where 2 and 3 is our Operands and ** is our Exponentiation Operator and 8 is our Result.

Exponentiation Example :


   document.write(2**3);

Results :

Increment Operator (++) :

Increment Operator adds one value to our Variable or Numeric value.
LIKE: 5++ = 6
Here 5 is our Operand and ++ is our Increment Operator and 6 is our Operant value after increment. But here is two types of Increment Operators

postFix Increment Operators (a++):

postFix Increment Operators returns the Incremented value after the Execution.
so if store value like :
var a=5;
var b=a++;
Then in that variable the JavaScript program will store the value first in the Variable called b and after that program will increment the value of variable called a.
so var b=a++; result b=5; That's why we will have 5 in our variable called a because of Post Increment increment.

PostFix Increment Operator Example :


  var a,b;
  a=5;
  b=a++;
  document.write(" a="+a);
  document.write(" b="+b);	

Results :

NOTE : Hare + sign is JS Concatenation Method for joining String and Variable data.

preFix Increment Operators (++a):

preFix Increment Operators returns the Incremented value before the Execution.
so if store value like :
var a=5;
var b=++a;
Then in that variable (variable b) the JavaScript program will store the value for a (which is 5) and then for b JS program will increment the value of Variable a (++a) and after the Increment JS Program will store the increment value of Variable a in to the Variable B.
so var b=++a; result b=6; That's why we will have 6 in our variable called b because of Pre Increment.

PreFix Increment Operator Example :


  var a,b;
  a=5;
  b=++a;
  document.write(" a="+a);
  document.write(" b="+b);	

Results :

NOTE : Hare + sign is JS Concatenation method for joining String and Variable data.

Decrement Operator (++) :

Decrement Operator deduct one value to our Variable or Numeric value.
LIKE: 5-- = 4
Here 5 is our Operand and -- is our Decrement Operator and 4 is our Operant value after Deduction. But here is two types of Decrement Operators

postFix Decrement Operators (a--):

postFix Decrement Operators returns the Decrement value after the Execution.
so if store value like :
var a=5--;
Then in that variable the JavaScript program will store the value first in the Variable called a and after that program will Decrement the value.
so var a=5--; result a=5; That's why we will have 5 in our variable called a because of Post Decrement.

POSTfix Decrement Operator Example :


  var a,b;
  a=5;
  b=a--;
  document.write(" a="+a);
  document.write(" b="+b);	

Results :

NOTE : Hare + sign is JS Concatenation Method for joining String and Variable data.

preFix Decrement Operators (++a):

preFix Decrement Operators returns the Deducted value before the Execution.
so if store value like :
var a=5;
var b=--a;
Then in that variable (variable b) the JavaScript program will store the value for a (which is 5) and then for b JS program will Decrement the value of Variable a (++a) and after the Decrement JS Program will store the Decremented value of Variable a in to the Variable B.
so var b=--a; result b=4; That's why we will have 4 in our variable called b because of Pre Decrement.

PreFix Decrement Operator Example :


  var a,b;
  a=5;
  b=--a;
  document.write(" a="+a);
  document.write(" b="+b);	

Results :

NOTE : Hare + sign is JS Concatenation method for joining String and Variable data.

Arithmetic Operations in JavaScript Example


<html>
 <head>
  <title> Arithmetic Operations </title>
 </head>
 <body>

   <script type="text/javascript">

    var a,b,sum,sub,multi,div,rem,postInc,preInc,postDec,preDec,expon;
    a=7;
    b=4;
    sum=a+b;
    sub=a-b;
    multi=a*b;
    div=a/b;
    rem=a%b;
    postInc=a++;
    preInc=++a;
    postDec=a--;
    preDec=--a;
    expon=a**b;   

    document.write("Addition A + B ="+sum);
    document.write("<br/>Subtraction A - B ="+sub);
    document.write("<br/>multiplication A * B ="+multi);
    document.write("<br/>Division A / B ="+div);
    document.write("<br/>Remainder A % B ="+rem);
    document.write("<br/>PostFix Increment A++ ="+postInc);
    document.write("<br/>PreFix Increment ++A ="+preInc);
    document.write("<br/>PostFix Decrement A-- ="+postDec);
    document.write("<br/>PreFix Decrement --A ="+preDec);
    document.write("<br/>Exponentiation A**B"+expon);

   </script>
 </body>
</html>	

Results


Comments

free minecraft download

http://www.nirvanamasaj.info/author/bernardh202/
02:36 pm

I appreciate, result in I found just what I was taking a look for. You have ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye

how to download minecraft

http://tinyurl.com/y3b99ar4
02:53 pm

It's the best time to make a few plans for the future and it's time to be happy. I've learn this publish and if I could I wish to suggest you few attention-grabbing things or tips. Perhaps you can write next articles regarding this article. I wish to learn even more things approximately it!

download minecraft

http://tinyurl.com/y2y6vdmu
10:45 am

Useful info. Fortunate me I found your website by chance, and I am surprised why this accident did not happened earlier! I bookmarked it.

g

http://tinyurl.com/y3gay5vf
04:17 am

Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and help others like you helped me.

minecraft free download

http://tinyurl.com/y22kc8tx
08:06 pm

An outstanding share! I have just forwarded this onto a colleague who has been conducting a little homework on this. And he in fact bought me lunch due to the fact that I stumbled upon it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanx for spending some time to discuss this matter here on your website.

g

http://bit.ly/2W3j1qB
03:29 am

An interesting discussion is definitely worth comment. I think that you need to write more about this subject matter, it may not be a taboo matter but typically people don't speak about such issues. To the next! Best wishes!!

minecraft download free pc

http://tinyurl.com/yy7r55w4
02:53 pm

I truly love your website.. Excellent colors & theme. Did you create this web site yourself? Please reply back as I'm trying to create my own site and would love to learn where you got this from or just what the theme is called. Thank you!

free minecraft download

http://tinyurl.com/y2axw9tv
08:30 pm

It's awesome for me to have a web page, which is helpful designed for my experience. thanks admin

free minecraft

http://j.mp/2LpECFj
08:13 am

magnificent issues altogether, you just received a emblem new reader. What might you recommend in regards to your put up that you simply made some days in the past? Any sure?

minecraft download pc

http://tinyurl.com/yy5nerzp
05:34 am

Hi there, I desire to subscribe for this webpage to take hottest updates, so where can i do it please assist.

gamefly

http://tinyurl.com/yxetzjr6
06:08 pm

Great post. I was checking continuously this blog and I'm impressed! Very helpful info specifically the last part :) I care for such information a lot. I was looking for this particular information for a very long time. Thank you and good luck.

gamefly

http://t.co/P92kf8Nt02
01:13 pm

I visited multiple sites but the audio feature for audio songs current at this web page is really superb.

gamefly

http://tinyurl.com/y3ej38dl
04:54 pm

Hmm is anyone else having problems with the pictures on this blog loading? I'm trying to figure out if its a problem on my end or if it's the blog. Any feedback would be greatly appreciated.

01:17 pm

substraction wrong

Write to Us