JavaScript Arithmetic Operator

In JavaScript, we can sum, subtract, or any operation between two values. These values are called operands and symbols are called operators. The arithmetic operation works with both literals and variables. Below is a constant values-based example,

let a = 10 + 5;
console.log(a);
// Output: 15

Following is a variable-based example,

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

In this article, we will learn the arithmetic operators of JavaScript and how to use them.

Arithmetic Operators of JavaScript

Sometimes, in our web application, we do mathematical operations, so these are the arithmetic operators that help us in calculation. There are eight kinds of arithmetic operators included in JavaScript, below is the list.

  • Subtraction ( – )
  • Addition ( + )
  • Multiplication ( * )
  • Division ( / )
  • Increment ( ++ )
  • Decrement ( — )
  • Exponentiation ( ** )
  • Modulus ( % )

1. Subtraction

In our everyday life, we use the minus or subtract symbol ( – ) to reduce the number from a numeric value. Similarly in JavaScript, we use subtract symbol for subtraction and JavaScript named it the subtraction operator. Below is an example

let a = 10 - 5;
console.log(a);
// Output: 5

In the above example, 5 decreases from 10 and assigns to variable a that has the let keyword. Then the console.log() function prints the result of the variable in the browser console.

2. Addition

To add a numeric value to another numeric value we use the plus symbol ( + ) which is called the addition operator.

let a = 10 + 5;
console.log(a);
// Output: 15

In this example, 5 adds into 10 through the plus symbol. Because when JavaScript finds a plus symbol between two values then it adds one into another and returns the result. After, the console.log() function simply prints the variable into the browser that has the result of two values.

3. Multiplication

In everyday life, we use the x symbol for multiplication but in JavaScript, we use the steric symbol ( * ) for multiplication.

let a = 5 * 10;
console.log(a);
// Output: 50

In this example, first operand 5 added in itself 10 times. Then the result is stored in the variable with the let keyword.

4. Division

In JavaScript, we use forward slash symbol ( / ) for division. Let’s divide a numeric value.

let a = 10 / 5;
console.log(a);
// Output: 2

In this example, the first operand 10 is simply divided by 5 which returns 2.

5. Increment

Increment operator adds 1 to existing numeric value when it runs. It uses the double plus symbol (++).

let a = 2;
a++;
console.log(a);
// Output: 3

In this example, 2 is assigned to variable a, and in the next line, it has an increment operator attached that increases 1 into the variable.

6. Decrement

Decrement reduces 1 from the existing numeric value when it runs. It uses double minus symbol( – – ).

let a = 8;
a--;
console.log(a);
// Output: 7

In this example, a numeric value 8 is assigned to the variable, and in the next line, it has a decrement operator that decreases 1 from the variable.

7. Exponentiation

The exponentiation operator uses double steric (**), in JavaScript, it is a multiplying symbol.

let a = 3 ** 2;
console.log(a);
// Output: 9

In this example, the first operand 3 raises the power of the second operand 2 which returns 9. It returns the same result as JavaScript’s pow() function. Following is an example,

let a = Math.pow(3,2);
console.log(a);
// Output: 9

8. Modulus

The Modulus operator (%) is also called a reminder. The modulus operator returns the leftover (reminder) after the division of two the numerical values. Following are examples,

console.log(17 % 5) 
// Output: 2 
console.log(42 % 10)
// Output: 2 
console.log(5 % 2) 
// Output: 1

You have the number 5 and need to know if it is odd or even programmatically so divide it as per rule 5 % 2 reminders will be 1 which means 5 is an odd.