Arithmetic Operators
Learn how to perform mathematical calculations in JavaScript using arithmetic operators.
What are Arithmetic Operators?
Arithmetic operators are symbols used to perform mathematical calculations with numeric values.
JavaScript provides arithmetic operators for addition, subtraction, multiplication, division, remainder, and exponentiation.
Arithmetic Operators
Addition
Adds two values.
Subtraction
Subtracts one value from another.
Multiplication
Multiplies two values.
Division
Divides one value by another.
Remainder
Returns the remainder after division.
Exponentiation
Raises a number to a power.
How Arithmetic Operators Work
Example 1: Basic Arithmetic Operations
We can use arithmetic operators directly with numeric values or store the values inside variables.
const firstNumber = 20;
const secondNumber = 5;
const addition = firstNumber + secondNumber;
const subtraction = firstNumber - secondNumber;
const multiplication = firstNumber * secondNumber;
const division = firstNumber / secondNumber;
console.log("Addition:", addition);
console.log("Subtraction:", subtraction);
console.log("Multiplication:", multiplication);
console.log("Division:", division);
Output
Subtraction: 15
Multiplication: 100
Division: 4
Example 2: Remainder and Exponentiation
The remainder operator (%) returns the value left after division. The exponentiation operator (**) is used to calculate powers.
const students = 27;
const groups = 5;
const studentsPerGroup = Math.floor(students / groups);
const remainingStudents = students % groups;
const powerResult = 2 ** 3;
console.log("Students Per Group:", studentsPerGroup);
console.log("Remaining Students:", remainingStudents);
console.log("2 Power 3:", powerResult);
Output
Remaining Students: 2
2 Power 3: 8
Important Note
Understanding the Remainder Operator
The % operator returns the remainder after division.
For example, 17 % 5 gives 2 because 2 remains after dividing 17 by 5.
Summary
Arithmetic operators are used for mathematical calculations in JavaScript. The main operators are + for addition, - for subtraction, * for multiplication, / for division, % for remainder, and ** for exponentiation.