  # Arithmetic Operators in JavaScript

Module - 3 JavaScript Operators
Arithmetic Operators in JavaScript

### Introduction

Imagine you are a web developer building a website for an online store. You want to create a feature that calculates the total price of items in the shopping cart. To do this, you need to use arithmetic operators in JavaScript. You start by adding the price of each item in the cart using the addition operator (+), and then multiply the total by the sales tax using the multiplication operator (*). Finally, you display the total price to the user using the console.log() method. As you work on this feature, you realize the importance of arithmetic operators in many real-life.

Arithmetic operators are the fundamental mathematical operators used in programming to carry out calculations on numerical data. In JavaScript, arithmetic operators can be utilized to perform addition, subtraction, multiplication, division, and modulo operations. These operations can be leveraged to solve sophisticated mathematical problems, manipulate data, and build interactive web applications.

JavaScript Arithmetic Operators

There are six arithmetic operators in JavaScript, which are:

2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%)
6. Exponentiation ()

The addition operator (+) is used to add two or more values in JavaScript. For example:

``````var a = 5;
var b = 10;
var c = a + b;
console.log(c); // Output: 15
``````

In this example, we add two variables a and b using the addition operator (+) and assign the result to a new variable c. The console.log() method is used to display the result on the console.

### Subtraction Operator

The subtraction operator (-) is used to subtract one value from another value in JavaScript. For example:

``````var a = 10;
var b = 5;
var c = a - b;
console.log(c); // Output: 5
``````

In the example above, we are subtracting the value of b from a using the subtraction operator (-) and assigning the result to a new variable c.

### Multiplication Operator

The multiplication operator (*) is used to multiply two or more values in JavaScript. For example:

``````var a = 5;
var b = 10;
var c = a * b;
console.log(c); // Output: 50
``````

In the example above, we are multiplying two variables a and b using the multiplication operator (*) and assigning the result to a new variable c.

### Division Operator

The division operator (/) is used to divide one value by another value in JavaScript. For example:

``````var a = 10;
var b = 5;
var c = a / b;
console.log(c); // Output: 2
``````

In the example above, we are dividing the value of a by b using the division operator (/) and assigning the result to a new variable c.

### Modulus Operator

The modulus operator (%) is used to find the remainder of the division of one value by another value in JavaScript. For example:

``````var a = 10;
var b = 3;
var c = a % b;
console.log(c); // Output: 1
``````

In the example above, we are finding the remainder of the division of a by b using the modulus operator (%) and assigning the result to a new variable c.

### Exponentiation Operator

The exponentiation operator () is used to raise one value to the power of another value in JavaScript. For example:

``````var a = 2;
var b = 3;
var c = a  b;
console.log(c); // Output: 8
``````

In the example above, we are raising the value of a to the power of b using the exponentiation operator () and assigning the result to a new variable c.

### Increment and Decrement Operators:

JavaScript also provides two special operators, the increment (++) operator and the decrement (--) operator. These are unary operators that can be used to increase or decrease the value of a variable by 1.

Increment Operator

The increment operator (++) is used to increase the value of a variable by 1. For example:

``````var a = 5;
a++;
console.log(a); // Output: 6
``````

In the example above, the value of variable a is increased by 1 using the increment operator (++).

Decrement Operator

The decrement operator (--) is used to decrease the value of a variable by 1. For example:

``````var a = 5;
a--;
console.log(a); // Output: 4
``````

In the example above, the value of variable a is decreased by 1 using the decrement operator (--).

### Real World Applications:

1. Building Interactive Web Applications: Arithmetic operators are commonly used in web development to create interactive applications that require mathematical calculations, such as calculators, games, and financial tools.
2. Analyzing Data: Arithmetic operators are used in data analysis to perform mathematical calculations on numerical data, such as calculating averages, standard deviations, and other statistical measures.
3. Automating Tasks: Arithmetic operators are used in programming to automate tasks that require mathematical calculations, such as generating reports, processing data, and performing simulations.

### Best practices

1. Use parentheses: When performing multiple operations, it is important to use parentheses to ensure that the calculations are performed in the desired order of precedence. For example, if you want to add two numbers and then multiply the result by a third number, you would write:
``````var result = (num1 + num2) * num3;
``````

This ensures that the addition operation is performed first before the multiplication operation.

1. Use the correct operator: It is important to use the correct operator for the intended operation. For example, when dividing two numbers, you should use the division operator (/) instead of the modulus operator (%).
2. Use shorthand operators: Shorthand operators such as +=, = and = can be used to shorten the code and make it more efficient. For example, instead of writing x = x + 1, you can write x += 1.
3. Use appropriate data types: When working with numbers in JavaScript, it is important to use the appropriate data type to avoid unexpected results. For example, if you want to perform calculations with decimal numbers, you should use the float or double data type instead of the integer data type.

### Conclusion

In JavaScript, arithmetic operators are essential tools for performing mathematical calculations and manipulating numerical data. By understanding these operators and how they work, developers can create powerful and interactive web applications, analyze complex data sets, and automate a wide range of tasks.

Open Compiler
###### Recommended Courses
Masters in Computer Science: Software Engineering  20,000 people are doing this course
Join India's only Pay after placement Master's degree in Computer Science. Get an assured job of 5 LPA and above. Accredited by ECTS and globally recognised in EU, US, Canada and 60+ countries.
Certification in Full Stack Web Development  20,000 people are doing this course
Become a job-ready Full Stack Web Developer in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.

Related Tutorials   696   561   1435   823

Related Articles JavaScript Animation Effects: Explained with Examples  6 mins  7328 Applications of JavaScript: A Guide to JavaScript’s uses  4 mins  3119 Reverse a Linked List: Algorithm, Examples & Best Practices  12 mins  2373 React.Js vs. Angular.Js: Which is a better Frontend Framework  7 mins  3369 Single Page Application in Web Development: Benefits and Examples  8 mins  2434 Reasons to choose React Native for Building Responsive Apps  7 mins  2766

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter. Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter

Vikash SrivastavaCo-founder & CPTO AlmaBetter Related Tutorials to watch  Made with  in Bengaluru, India