programminghead Logo

Arithmetic Operator in JavaScript

Arithmetic Operators : Introduction

Arithmetic Operators in JavaScript are used to perform arithmetic Operations between two Values or "Operand", Perform Arithmetic Operation according to given "Operator" and return one value as a Result.

Operand Operator Operand Result
2 + 5 = 7

Arithmetic Operators Example


 <body>
  <script>
  
    document.write(2+2);
    document.write(2-2);
    document.write(2*2);
    document.write(2/2);
 
  </script>
 </body>

Results


Standard Arithmetic Operators :

There are Four standard Arithmetic Operators :

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

Arithmetic Operators in JavaScript Image

Addition Operator in JavaScript(+) :-

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 in JavaScript (-) :

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 in JavaScript (*) :

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 in JavaScript (/) :

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 in JavaScript (%) :

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 in JavaScript (**) :

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 in JavaScript (++) :

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 in JavaScript (++) :

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

Write to Us

Captcha