EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Arithmetic Operators in JavaScript

By Yashi GoyalYashi Goyal

Home » Software Development » Software Development Tutorials » JavaScript Tutorial » Arithmetic Operators in JavaScript

Arithmetic Operators in JavaScript

Introduction to Arithmetic Operators in JavaScript

In the era of web development, javascript is one of the most popular and commonly used programming languages used by programmers. It is a client-side language and has a very large library with many inbuilt functions created for the ease of programming. Like other programming languages, Javascript also allows programmers to perform various operations on numbers. There are many types of Operators used in Javascript like Arithmetic Operators, Bitwise Operator, Assignment Operators, Comparison Operators, etc. Arithmetic Operators take numerical values either variables or literals and perform the operation on its operands to produce the final result. Generally, arithmetic operators work in the same way in Javascript like other programming languages except for the division operation.

Types of Arithmetic Operators with Examples

The most commonly used operators in any programming language are the Arithmetic ones. There are various Arithmetic Operators used in Javascript in order to perform the operations between the two or more numerical values or the variables holding those numerical values. Arithmetic Operators used in Javascript along with their examples are given below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Addition

An addition operator produces the sum of two or more numerical values or the variables holding those numerical values. In the case of strings, the addition operator does the String concatenation.

Example #1

This is the example of the addition of 2 numerical values.

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Addition of numerical values</p>
<p id="p1"></p>
<script>
var sum = 70 + 30;
document.getElementById("p1").innerHTML = sum;
</script>
</body>
</html>

Output:

Arithmetic Operators in JavaScript eg1.1

In the above example, no variables are used to perform the addition between the 2 numbers instead addition of these 2 numbers are assigned directly to a variable named ‘sum’ and the value returned by the ‘sum’ is printed in the paragraph.

Popular Course in this category
JavaScript Training Program (39 Courses, 23 Projects, 4 Quizzes)39 Online Courses | 23 Hands-on Projects | 225+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (6,048 ratings)
Course Price

View Course

Related Courses
Angular JS Training Program (9 Courses, 7 Projects)Vue JS Training (1 Courses, 3 Project)
Example #2

This is the example of the addition of 2 string values.

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Addition of String values</p>
<p id="p1"></p>
<script>
var name = "rakesh" + " " + "yadav";
document.getElementById("p1").innerHTML = name;
</script>
</body>
</html>

Output: 

Arithmetic Operators in JavaScript eg1.2

In the above example, ‘rakesh’ and ‘yadav’ are the 2 strings and the variable ‘name’ is concatenating these 2 strings using the simple ‘+’ sign. So printing the value of variable ‘name’ in the paragraph ‘p’ is returning the full name as ‘rakesh yadav’.

2. Subtraction

As it is genuine that subtraction operator is used for the subtraction of 2 numbers or the variables holding those numbers. The operator used is ( – ).

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Subtraction of numerical values</p>
<p id="p1"></p>
<script>
var diff = 70 - 30;
document.getElementById("p1").innerHTML = diff;
</script>
</body>
</html>

Just like in addition, no extra variables are used to hold the values. Instead, subtraction expression is written and its value is assigned to a variable named ‘diff’. The final result of the difference is to get printed in the paragraph using the variable ‘diff’.

Output:

Arithmetic Operators in JavaScript eg2

3. Multiplication

Multiplication operator is used for the multiplication of values (either numbers or the variables holding those values).

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Multiplication of numerical values</p>
<p id="p1"></p>
<script>
var num1 = 7;
var num2 = 3;
var mult = num1 * num2;
document.getElementById("p1").innerHTML = mult;
</script>
</body>
</html>

Output:

Arithmetic Operators in JavaScript eg3

In the above example, ‘num1’ and ‘num2’ are the 2 variables used in order to hold the values and the multiplication is performed between them using the (*) operator and its result gets assigned to a variable ‘mult’. The value of the variable ‘mult’ is then displayed in the HTML paragraph.

4. Division

Division operator is used for the division of the 2 numbers where the left operand is dividends and the right operand is the divisor. Operator used for division is ( / ). Unlike other programming languages like C, Java, etc. Javascript produces the floating value as output instead of an integer.

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Division of numerical values</p>
<p id="p1"></p>
<script>
var div = 70 / 30;
document.getElementById("p1").innerHTML = div;
</script>
</body>
</html>

Output:

Arithmetic Operators in JavaScript eg4

In the above example, the ‘div’ variable is used and the result of 2 numerical values produced using the (/) operator is assigned to it. The result of ‘div’ is then displayed in the HTML paragraph.

5. Modulus

The modulus operator returns the remainder returned on dividing the 2 numbers.

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Modulus returned on division of numerical values</p>
<p id="p1"></p>
<script>
var num1 = 70;
var num2 = 30;
var mod = 70 % 30;
document.getElementById("p1").innerHTML = mod;
</script>
</body>
</html>

Output:

eg5

In the above example, modulus or remainder is returned using the (%) operator and the results are assigned to a variable ‘mod’ whose value gets displayed in the paragraph.

6. Increment

There are some operators in Javascript which work on a single operand, which means instead of 2 operands which are required in operations like addition, subtraction, multiplication, division, etc. Instead, unard operator uses only a single operand or variable to perform an operation. The increment is a unary operator used in Javascript and increments the value of the variable by 1. ++ sign is added after the name of the variable in order to increment its value by 1.

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Increment of a variable </p>
<p id="p1"></p>
<script>
var number = 70;
number++;
document.getElementById("p1").innerHTML = number;
</script>
</body>
</html>

Output:

eg7

In the above example, the value of the variable named ‘number’ is incremented by using the (++) sign after the variable name. The incremented value of the above variable is then displayed in the HTML paragraph.

7. Decrement

This operator is just the opposite of the Increment operator. It is also a unary operator and takes only 1 variable holding the value to perform its operation. It decrements the value of the variable by 1.

Code:

<!DOCTYPE html>
<html>
<body>
<p>This is an example of Decrement of a numerical value</p>
<p id="p1"></p>
<script>
var number = 70;
number --;
document.getElementById("p1").innerHTML = number;
</script>
</body>
</html>

Output:

eg6

In the above example, the value of the variable named ‘number’ is decremented by using the (–) sign after the variable name. The decremented value of the above variable is then displayed in the HTML paragraph.

Conclusion

The above explanation clearly describes the arithmetic operators used on javascript. There are various types of operators and each type of operator has its own purpose. These operators are very basic concepts in any programming language and are taught in the starting only to the newbies before digging into the deep concepts. So it is very important to understand their use and purpose thoroughly as they form the basis of understanding for a programmer.

Recommended Articles

This is a guide to Arithmetic Operators in JavaScript. Here we discuss the types of Arithmetic Operators in JavaScript along with examples and code implementation. You can also go through our suggested articles to learn more –

  1. Control Statement in JavaScript
  2. JavaScript Arrow Function
  3. Continue in JavaScript
  4. JavaScript Events

JavaScript Training Program (39 Courses, 23 Projects)

39 Online Courses

23 Hands-on Projects

225+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
JavaScript Tutorial
  • Operators
    • Arithmetic Operators in JavaScript
    • JavaScript Assignment Operators
    • Logical Operators in JavaScript
    • Comparison Operators in JavaScript
    • Bitwise Operators in JavaScript
    • Ternary Operator JavaScript
    • Boolean Operators in JavaScript
    • JavaScript Boolean()
    • Unary Operators in JavaScript
    • JavaScript Number
    • JavaScript Floating
    • JS Operator Precedence
  • Basic
    • Introduction To JavaScript
    • What is JavaScript
    • What Javascript Can Do
    • Uses of JavaScript
    • How JavaScript Works
    • Is Javascript Case Sensitive
    • Is Javascript Object Oriented
    • Features of JavaScript
    • Careers in JavaScript
    • How to Install JavaScript
    • Best Javascript Frameworks
    • JavaScript MVC Frameworks
    • JavaScript Tools
    • What is JSON
    • What is Redux
    • What is ES6
    • Uses of React JS
    • How Analytics.JS Works
    • JavaScript Compilers
    • Java and JavaScript
    • JavaScript Literals
    • Variables in JavaScript
    • JavaScript Global Variable
    • JavaScript Keywords
    • String in JavaScript
    • Pointers in JavaScript
    • Primitive Data Types in JavaScript
    • JavaScript console log
    • Object in JavaScript
    • JavaScript Enum
    • Arithmetic in JavaScript
    • Assignment Operator in JavaScript
    • JavaScript Modules
    • Cheat Sheet JavaScript
    • NPM Alternatives
  • Objects
    • JavaScript Objects
    • JavaScript History Object
    • JavaScript Object Notation
    • JavaScript Map Object
    • JavaScript Date Object
    • JavaScript Window Object
    • JavaScript Object Constructors
    • JavaScript Clone Object
    • JavaScript Object.assign()
    • JavaScript object.is()
    • JavaScript Object to JSON
  • Control statements
    • Control Statement in JavaScript
    • Conditional Statements in JavaScript
    • Break Statement in JavaScript
    • Continue in JavaScript
    • Switch Statement in JavaScript
    • Case Statement in JavaScript
    • JavaScript if Statement
    • Nested if in JavaScript
    • JavaScript elseIf
  • Loops
    • For Loop in JavaScript
    • While Loop in JavaScript
    • Do While Loop in JavaScript
    • Nested Loop in JavaScript
  • Array
    • Arrays in JavaScript
    • Multi-Dimensional Array in JavaScript
    • Associative Array in JavaScript
    • JavaScript Declare Array
    • Arrays Methods in JavaScript
    • JavaScript Loop Array
    • String Array in JavaScript
    • JavaScript Get Array Length
    • JavaScript Merge Arrays
    • JavaScript Array Sort
    • JavaScript Array Push
    • JavaScript Iterate Array
    • JavaScript Empty Array
    • JavaScript Array Concat
    • Dynamic Array in JavaScript
    • JavaScript subarray()
    • JavaScript Array Filter
    • JavaScript Nested Array
    • JavaScript Flatten Array
    • JavaScript Array map()
    • JavaScript Array includes()
    • JavaScript Array Contain
    • JavaScript Array Slice
    • JavaScript Copy Array
    • Javascript Sum Array
    • JavaScript reverse Array
    • JSON Parse Array
  • Sorting
    • Sorting Algorithms in JavaScript
    • Insertion Sort in JavaScript
    • Merge Sort in JavaScript
    • Quick Sort in JavaScript
    • Bubble Sort in JavaScript
    • pop() in JavaScript
    • push() in JavaScript
  • Functions
    • JavaScript String Functions
    • JavaScript String Length
    • JavaScript split String
    • JavaScript Math Functions
    • Recursive Function in JavaScript
    • Regular Expressions in JavaScript
    • JavaScript Arrow Function
    • JavaScript Date Function
    • Match Function in Javascript
    • Replace Function in JavaScript
    • JavaScript Call Function
    • JavaScript Pass By Value
    • split() Function in JavaScript
    • reduce() Function JavaScript
    • JavaScript String replace
    • JavaScript Compare Strings
    • JavaScript Sleep
    • JavaScript toLowercase()
    • JavaScript String to Float
    • JavaScript String to Number
    • JavaScript String to int
    • JavaScript Object to String
    • JavaScript Convert to JSON
    • JavaScript Append
    • Javascript Array to String
    • Javascript Nested Functions
    • Set in JavaScript
    • Vectors in JavaScript
    • Javascript Anonymous Function
    • sign() in JavaScript
    • isNaN() JavaScript
    • Slice() Method in JavaScript
    • Javascript void
    • endsWith() in JavaScript
    • trim() Function in JavaScript
    • JavaScript typeof
    • JavaScript indexOf()
    • JavaScript encodeURI()
    • JavaScript Random
    • JavaScript z-index
    • JavaScript Absolute Value
    • JavaScript Closure
    • Javascript Prototype
    • JavaScript Date parse
    • JavaScript DatePicker
    • JavaScript Parse String
    • JavaScript undefined
    • JavaScript FileReader
    • JavaScript Style visibility
    • JavaScript sleep Function
    • JavaScript forEach()
    • JavaScript keys()
    • JavaScript keycodes
    • JavaScript find() 
    • JavaScript values()
    • JavaScript Counter
    • JavaScript Countdown
    • JavaScript instanceof
    • JavaScript Delay
    • JavaScript Default Value
    • JavaScript concat String
    • JavaScript Document Object Model
    • Unshift JavaScript
    • JavaScript Callback Function
    • JavaScript hasOwnProperty()
    • JavaScript UUID
    • JSON Parser
    • JSON Array of Strings
    • Sublime Pretty JSON
    • JavaScript JSON to string
    • JavaScript Uppercase
    • JavaScript Namespace
    • JavaScript Range
    • JavaScript JSON
    • JavaScript exec()
    • JavaScript test()
    • JavaScript Self Invoking Functions
    • JSON Stringify Pretty
    • JavaScript findIndex()
    • JavaScript entries()
    • JavaScript join()
    • JavaScript lastIndexOf()
    • JavaScript every()
    • JavaScript getElementById()
    • JavaScript getElementsByName()
    • JavaScript getElementsByTagName()
    • JavaScript getElementsByClassName()
    • JavaScript Animation
    • JavaScript Minify
  • Events
    • JavaScript Events
    • JavaScript Event Handler
    • JavaScript Keyboard Events
    • JavaScript Mouse Events
    • JavaScript mousemove
    • JavaScript mousedown
    • JavaScript onchange
    • JavaScript onmouseout
    • JavaScript Onkeydown
    • JavaScript onsubmit
    • JavaScript Form Events
    • JavaScript Window Events
    • JavaScript Custom Events
    • JavaScript Alert
    • JavaScript Confirm
    • JavaScript onclick Alert
    • JavaScript Apply
    • JavaScript onblur
    • JavaScript onkeyup
    • JavaScript onfocus
  • Advanced
    • Constructor in JavaScript
    • Inheritance in JavaScript
    • Polymorphism in JavaScript
    • JavaScript Static Method
    • Navigator in JavaScript
    • Abstract Classes in JavaScript
    • keyword in JavaScript
    • Overriding in JavaScript
    • JavaScript Grid
    • JavaScript innerText
    • Cookies in JavaScript
    • Delete Cookie in JS
    • Encapsulation in JavaScript
    • JavaScript Parent Node
    • JavaScript Parent
    • Javascript Remove Element
    • Errors in JavaScript
    • Forms in JavaScript
    • JavaScript Validator
    • JavaScript Form Validation
    • Email Validation in JavaScript
    • Hamburger Menu JavaScript
    • JavaScript Date Formats
    • JavaScript Number Format
    • JavaScript Debugger
    • JavaScript Stack
    • Queue in JavaScript
    • parseFloat in JavaScript
    • Javascript innerHTML
    • JavaScript setInterval
    • JavaScript Popup Box
    • JavaScript Message Box
    • Lightbox in JavaScript
    • Javascript Design Patterns
    • addEventListener JavaScript
    • Timer in JavaScript
    • JavaScript setTimeout
    • JavaScript clearTimeout()
    • JavaScript String Format
    • JavaScript Scroll to Top
    • JavaScript Function Declaration
    • File Handling in JavaScript
    • JavaScript parseInt
    • JavaScript prompt
    • JavaScript Set Class
    • JavaScript Try Catch
    • Javascript Throw Exception
    • Finally in JavaScript
    • JavaScript Get Element by Class
    • JavaScript Obfuscator
    • Disable JavaScript
    • SetAttribute JavaScript
    • JavaScript Cursor
    • LinkedList in JavaScript
    • JavaScript WeakMap
    • JavaScript DOM
    • JavaScript defer
    • JavaScript Promise
    • JavaScript Refresh Page
    • JSON.stringify JavaScript
    • JavaScript IIFE
  • Programs
    • Patterns in JavaScript
    • Reverse in JavaScript
    • Palindrome in JavaScript
    • Factorial Program in JavaScript
    • Fibonacci Series In JavaScript
    • Square Root in JavaScript
    • Prime Number in JavaScript
    • Armstrong Number in JavaScript
    • Random Number Generator in JavaScript
    • Reverse String in JavaScript
    • JavaScript Random String
    • Functional Programming in JavaScript
  • Interview Questions
    • Javascript Interview Questions
    • JSON Interview Questions
    • JS Interview Questions

Related Courses

JavaScript Certification Training

Angular JS Certification Training

Vue JS Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - JavaScript Training Program (39 Courses, 23 Projects) Learn More