Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

JavaScript Arithmetic Operators

by suresh

The JavaScript Arithmetic Operators include operators like Addition, Subtraction, Multiplication, Division, and Modulus. All these operators are binary operators, which means they operate on two operands.

The below table shows the Arithmetic Operators in JavaScript with examples.

JS Arithmetic OperatorsOperationExample
+Addition10 + 2 = 12
–Subtraction10 – 2 = 8
*Multiplication10 * 2 = 20
/Division10 / 2 = 5
%Modulus – It returns the remainder after the division10 % 2 = 0 (Here remainder is zero). If it is 10 % 3 then it will be 1.

JavaScript Arithmetic Operators Example

For this JS Arithmetic operations example, We are using two variables a and b, and their values are 12 and 3.

We are going to use these two variables to perform various arithmetic operations in JavaScript Programming Language

<!DOCTYPE html>
<html>
<head>
    <title>Javascript Arithmetic Operators</title>
</head>
<body>
    <h1>Performing Arithmetic Operations </h1>
<script>
   var a = 12, b = 3;
   var addition, subtraction, multiplication, division, modulus;

   addition = a + b; //addition of 3 and 12
   subtraction = a - b; //subtract 3 from 12
   multiplication = a * b; //Multiplying both
   division = a / b; //dividing 12 by 3 (number of times)
   modulus = a % b; //calculation the remainder

   document.write("Addition of " + a +' and ' + b +" is = " + addition + "<br />");
   document.write("Subtraction of " + a +' and ' + b +" is = " + subtraction + "<br />");
   document.write("Multiplication of " + a +' and ' + b +" is = " + multiplication + "<br />");
   document.write("Division of " + a +' and ' + b +" is = " + division + "<br />");
   document.write("Modulus of " + a +' and ' + b +" is = " + modulus + "<br />");
</script>
</body>
</html>

OUTPUT

JavaScript Arithmetic Operators 1

JavaScript Arithmetic Operations Example 2

For this JS example also, We are using two variables a and b and their values are 12 and 3.

Here. we are going to use these variables to show you, How to display the JavaScript output in Paragraphs.

<!DOCTYPE html>
<html>
<head>
    <title>Javascript Arithmetic Operators 2</title>
</head>
<body>
    <h1>Performing Arithmetic Operations </h1>
    <p id = 'add'> Addition </p>
    <p id = 'sub'> Subtraction </p>
    <p id = 'mul'> Multiplication </p>
    <p id = 'div'> Division </p>
    <p id = 'mod'> Modulus </p>
<script>
   var a = 12, b = 3;
   var add, sub, mul, div, mod;

   add = a + b; //addition of 3 and 12
   sub = a - b; //subtract 3 from 12
   mul = a * b; //Multiplying both
   div = a / b; //dividing 12 by 3 (number of times)
   mod = a % b; //calculation the remainder

   document.getElementById("add").innerHTML = "Addition of " + a +' and ' + b +" is = " + add;
   document.getElementById("sub").innerHTML = "Subtraction of " + a +' and ' + b +" is = " + sub;
   document.getElementById("mul").innerHTML = "Multiplication of " + a +' and ' + b +" is = "+ mul;
   document.getElementById("div").innerHTML = "Division of " + a +' and ' + b +" is = " +  div;
   document.getElementById("mod").innerHTML = "Modulus of " + a +' and ' + b +" is = " +  mod;
</script>
</body>
</html>

OUTPUT

JavaScript Arithmetic Operators 2

Placed Under: JavaScript

Trending Posts

SQL LAG Function

Lattice Scatter Plot in R

Java Program to find Volume and Surface Area of Cube

Python If Else

Query builder in SSIS

Input Parameters in SQL Stored Procedure

Execute Packages in SQL Server using SSIS Execute Package Task

Python List

MySQL IN Operator

C program to Toggle Case of all Characters in a String

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy