JavaScript Arithmetic Operators Tutorial

In this section, we will see what the Arithmetic operators are and how to use them in JavaScript.

Note: In this section, we’re assuming you already familiar with the JavaScript operators in general.

What is Arithmetic Operators in JavaScript?

There are multiple operators that can be used for basic mathematical operations. These operators are in the category of the Arithmetic operators.

List of Arithmetic Operators in JavaScript

In the table below you can see the list of these operators:

Operator Symbol
Addition Operator +
Subtraction Operator
Multiplication Operator *
Division Operator /
Exponentiation Operator **
Modulus Operator %
Increment Operator ++
Decrement Operator

JavaScript Addition `+` Operator

When we want to add two Number type values to each other, we can use the addition `+` operator. The operands of this operator can be primitive values of type BigInt and Number, a function call that results such type of value or an expression that results a numeric value.

Addition `+` Operator Syntax:

LeftOperand + RightOperand

Example: using Addition `+` operator in JavaScript

const res = 12300 + 3422;

console.log(res);

Output:

15722

The addition operator is also overridden to work with String values as well. This means if the type of one of the involved operands is `String`:

  1. The execution engine will coerce the other non-String operand into String type by running the `ToString` operation.
  2. After that, it will concatenate both operands on each other.

Example: Addition operator in JavaScript

const res = "12300" + 3422;

console.log(res);

Output:

123003422

If the operands are of type non-Number but also non-String, the execution engine will run the ToNumber operation on those operands to coerce them into a value of type Number.

Example: JavaScript Addition Operator

const res = true + false;

console.log(res);

Output:

1

JavaScript Subtraction `-` Operator

We use this operator when we want to subtract one value from another in the JavaScript.

Note: the involved operands can be either a constant value like (10, 4 ,2, 43.22 etc.), the result of calling a function or the value of a variable.

Subtraction `-` Operator Syntax:

LeftOperand - RightOperand

Example: using subtraction `-` operator in JavaScript

const numOne = 12300;

const numTwo = 3422;

const res = numOne - numTwo;

console.log(res);

Output:

8878

If the type of involved operands is something other than `Number`, the execution engine will run the `ToNumber` operation on those operands to coerce the type into Number.

Example: JavaScript subtraction operator

const numOne = "12300";

const numTwo = 3422;

const res = numOne - numTwo;

console.log(res);

Output:

8878

JavaScript Multiplication `*` Operator

When we want to multiply two values into each other, we use multiplication `*` operator.

Multiplication `*` Operator Syntax:

LeftOperand * RightOperand

Example: using multiplication `*` operator in JavaScript

const numOne = 23;

const numTwo = 34;

const res = numOne * numTwo;

console.log(res);

Output: 782

Note: if the type of the involved operands is not `Number`, the execution engine will invoke the `ToNumber` operation on those operands to coerce the type into Number.

Example: JavaScript multiplication operator

const numOne = 23;
const numTwo = 34;
const res = numOne * numTwo;
console.log(res);

Output: 6800

JavaScript Division `/` Operator

On the contrary, to the multiplication operator, this operator is used to divide the operand on the left side by the operand on the right side of the division `/` operator.

Division `/` Operator Syntax:

LeftOperand / RightOperand

Example: using division `/` operator in JavaScript

const numOne = 100;

const numTwo = 34;

const res = numOne / numTwo;

console.log(res);

Output: 2.9411764705882355

Note: if the type of the involved operands is not `Number` the execution engine will run the `ToNumber` operation on those operands to coerce the type into Number.

Example: JavaScript division operator

const obj = {
  valueOf(){
    return 200;
  }
}
const numTwo = 34;
const res = obj * numTwo;
console.log(res);

Output:

2.9411764705882355

JavaScript Exponentiation `**` Operator

This operator is used to raise the value of the left operand to the power of the right operand and then return the result.

Exponentiation `**` Operator Syntax:

LeftOperand ** RightOperand

Example: using exponentiation `**` operator in JavaScript

const res = 100 ** 2;

console.log(res);

Output: 10000

Note: just like the other operators you saw so far, if the operands involved is not of type Number, the execution engine will run the `ToNumber` operation on those operands to coerce the value into Number type.

Example: JavaScript Exponential Operator

const obj = {
   valueOf(){
     return 100;
   }
 }
 const numTwo = 34;
 const res = obj / numTwo;
 console.log(res);

Output: 10000

JavaScript Modulus `%` Operator

Let me ask a question: how many 3s are in 10?

If you look carefully, it is 3. This is because if we multiply 3 to 4 we will get 12 and which passes the value 10 by two, but if we multiply 3 by 3, the result becomes 9. Now if we subtract the value 10 from 9 what’s the remainder? It is 1.

We use modulus operator to get that remainder.

Modulus `%` Operator Syntax:

LeftOperand % RightOperand

Example: using modulus `%` operator in JavaScript

const res = 10 % 3;

console.log(res);

Output:

1

Note: the type coercion will also apply on the operands of this operator if they are of type other than `Number`.

JavaScript Increment `++` Operator

This operator is used to increase the value assigned to a variable by one.

Note: only those identifiers that are created via either `var` or `let` keyword can use this operator. This is because the value of that variable will be increased by one and so we can’t use an identifier that is created via the `const` keyword.

Remember: the value of the constant identifier won’t change.

This operator can be set on the left or right side of the target variable.

Increment `++` Operator Syntax:

++Operand

Operand++

Example: using increment `++` operator in JavaScript

var valueRight = 13;

var valueLeft = 13;

var left = ++valueLeft;

var right = valueRight++;

console.log(`left: ${left}, right: ${right}, valueRight: ${valueRight}`)

Output:

left: 14, right: 13, valueRight: 14

JavaScript Increment Operator Differences

If the `++` operator appeared on the left side of a variable like ` var left = ++valueLeft;` that means first increase the value assigned to the operand (in this case `valueLeft`) and then proceed to the rest of operations. So the assigned value to the `left` variable will be 14.

On the other hand, if the `++` operator appeared on the right side of a variable like ` var right = valueRight++;`, the value assigned to the operand (in this case `valueRight`) will be increased by one, but this is only guaranteed for the next time we call the target variable.

So the final value that is assigned to the variable `right` in this example is 13 because the value of the `valueRight` did not change right away and the `right` variable took the old value of the valueRight variable.

Note: if the type of the involved operand is not `Number`, the execution engine will coerce the target operand by running the `ToNumber` operation on it.

Example: JavaScript Increment Operator

var valueRight = '13';

var valueLeft = '13';

var left = ++valueLeft;

var right = valueRight++;

console.log(`left: ${left}, right: ${right}, valueRight: ${valueRight}`)

Output:

left: 14, right: 13, valueRight: 14

JavaScript Decrement `–` Operator

This operator is used to decrease the value assigned to a variable by one.

Notes:

  • This operator can be set on the left and right side of the target variable.
  • Rules that applied to `++` operator also apply to `–` operator.

Decrement `–` Operator Syntax:

--Operand

Operand--

Example: using decrement `–` operator in JavaScript

var valueRight = 13;

var valueLeft = 13;

var left = --valueLeft;

var right = valueRight-- + valueRight;

console.log(`left: ${left}, right: ${right}, valueRight: ${valueRight}`);

Output:

left: 12, right: 25, valueRight: 12

Note: The basic arithmetic operations mentioned in this section apply to operands from left to right.

For example, if we have `var res = 10/2; ` the value 10 is divided by 2 not vice versa!

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies