What are the 5 JavaScript Mathematical Operators?
Arithmetic Operators
Arithmetic operators perform arithmetic functions on numbers (literals or variables) and return a value.
JavaScript includes many operators from basic math as well as a few programming-specific operators.
There are five fundamental operations in mathematics: addition, subtraction, multiplication, division, and modulas.
Here is a reference table of JavaScript arithmetic operators.
Operator | Description | Example |
---|---|---|
+ | Addition | 25 + 5 = 30 |
– | Subtraction | 25 – 5 = 20 |
* | Multiplication | 10 * 20 = 200 |
/ | Division | 20 / 10 = 10 |
% | Modulus | 7 % 3 = 1 |
++ | Increment | let a = 10; a++; Now a = 11 |
– – | Decrement | let a = 10; a– ; Now a = 9 |
Basic Syntax
A typical arithmetic operation operates on two numbers.
The two numbers can be literals:
let sum = 100 + 80;
//Output 180
or variables:
let a = 100;
let b = 60;
let sum = a + b;
//Output 160
Operators and Operands
The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is defined by an operator.
Example
Operand | Operator |
---|---|
100 | + or – |
Addition
The addition operator (+
) adds numbers:
let x = 6;
let y = 2;
let z = x + y;
// Output 8
Subtracting
The subtraction operator (-
) subtracts numbers.
let x = 8;
let y = 2;
let z = x - y;
// Output 6
Multiplying
The multiplication operator (*
) multiplies numbers.
let x = 6;
let y = 2;
let z = x * y;
// Output 12
Remainder
The modulus (also known as remainder) operator (%
). The remainder operator returns the remainder left over when one operand is divided by a second operand.
let x = 9;
let y = 0;
let z = x % y;
// Output 0
As an example, we know that 3 goes into 9 exactly three times, and there is no remainder.
Incrementing
The increment operator (++
) increments numbers. Increment operators increase the numerical value by one.
let x = 5;
x++;
let z = x;
// Output 6
Decrementing
The decrement operator (—
) decrements numbers. Decrement operators reduce the numerical value by one.
let x = 9;
x--;
let z = x;
// Output 8
Conclusion
We covered arithmetic operators and syntax in this article, including many common mathematical operators as well as a few that are unique to programming.
Further reading
Find out more about Basic math in JavaScript — numbers and operators – Learn web development | MDN
See also
What exactly is JavaScript?
What are the Three Variables in JavaScript?
What are the Three Primitive JavaScript Data Types?
If you liked this article, then please share. You can also find me on Twitter for more updates.