What Are the Different Arithmetic Operators in JavaScript?
Working with Numbers and Arithmetic Operators
JavaScript provides tools to perform basic arithmetic operations on numbers, such as addition, subtraction, multiplication and division. JavaScript also include operators for more complex arithmetic operation, such as remainder and exponentiation.
All these tools are called arithmetic operators. Let’s look at these operators in details, how to use them and how to combine them.
The addition operator is a plus sign (+). The addition operator allow you to find the total of two or more numbers. In addition operations, the order of the numbers doesn’t matter:
const num1 = 10;
const num2 = 5;
const num3 = 15;
const result1 = num1 + num2;
const result2 = num2 + num1;
const result3 = num2 + num1 + num3;
console.log(result1); // 15
console.log(result2); // 15
console.log(result3); // 30The subtraction operator is a minus sign (-). I allow you to find the difference between two numbers. Use the minus sign when you want to subtract a number from another number, usually a smaller one from a bigger one:
const difference = 10 - 5;
console.log(difference); // 5If a small number comes first, you’ll get negative result:
const difference = 5 - 10;
console.log(difference); // -5You can also assign the number to variables and do the subtraction with the variable names:
const num1 = 10;
const num2 = 5;
const result = num1 - num2;
console.log(result); // 5In JavaScript, the multiplication operator is represented by an asterisk (*) and is used to find the product of two or more numbers. The order of the numbers you’re multiplying does not matter:
const num1 = 10;
const num2 = 5;
const num3 = 15;
const result1 = num1 * num2;
const result2 = num2 * num1;
const result3 = num2 * num1 * num3;
console.log(result1); // 50
console.log(result2); // 50
console.log(result3); // 750In JavaScript, the division operator is a slash (/), which differs from the division symbol used in traditional math (÷). You perform division operations with the division operator. The order of the numbers you’re diving matters in this case:
const num1 = 10;
const num2 = 5;
const num3 = 15;
const result1 = num1 / num2;
const result2 = num2 / num1;
const result3 = num2 / num1 / num3;
console.log(result1); // 2
console.log(result2); // 0.5
console.log(result3); // 0.03333333333333333It’s important to note that if you try to divide by zero, JavaScript will return Infinity:
const result = 10 / 0;
console.log(result); // InfinityMake sure to avoid those types of calculations so you don’t end up with unexpected results in your code.
The remainder operator, represented by a percentage sign(%), returns the remainder of a division. The remainder in math is the leftover value after performing division:
const num1 = 10;
const num2 = 3;
const remainder = num1 % num2;
console.log(remainder); // 1The exponentiation operator, represent by a double asterisk (**), raises one number o the power of another:
const num1 = 2;
const num2 = 3;
const exponent = num1 ** num2;
console.log(exponent); // 8It’s possible to mix operators in a single operation or expression:
const result = 10 + 5 * 2 - 8 / 4;
console.log(result); // 18When you mix different operators in a single expression, the JavaScript engine follows a system called operator precedence to determine the order of operations. Operator precedence determines the order in which operations are executed in expressions.


