JavaScript arithmetic operators are used to perform mathematical calculations on values called operands. You will use them often when adding totals, updating counters, calculating discounts, or building logic for forms, games, and apps.
Most arithmetic operators are binary operators, which means they work with two operands. A few are unary operators, which means they work with only one operand.
Operands can be:
let result1 = 3 + 5; // 3 and 5 are literal valuesconst x = 3; const y = 5; let result2 = x + y; // x and y are variables
let result3 = 3 + 2 * x; // 2 * x is part of an expression
In general, arithmetic operators are used for math, but some operators can do more. For example, the addition operator + can also join strings.
Here are the most common arithmetic operators in JavaScript:
| Operator | Name | Description |
|---|---|---|
+ |
Addition | Adds two operands |
- |
Subtraction | Subtracts the second operand from the first |
* |
Multiplication | Multiplies two operands |
/ |
Division | Divides the left operand by the right operand |
% |
Modulus | Returns the remainder after division |
++ |
Increment | Increases a value by 1 |
-- |
Decrement | Decreases a value by 1 |
let a = 10; let b = 3;console.log(a + b); console.log(a - b); console.log(a * b); console.log(a / b); console.log(a % b);
+) OperatorThe addition operator + adds two numeric values.
let x = 5; let y = 10; let sum = x + y; // 15let value1 = '10' + 3; // "103" let value2 = '10' + '3'; // "103"
+const x = 3; const y = 5;console.log(x + y); console.log('10' + 3); console.log('10' + '3');
-) OperatorThe subtraction operator - subtracts the right operand from the left operand.
20 - 10; // 10
'20' - 10; // 10
'20' - '10'; // 10
'20ee' - 10; // NaN
NaN.Infinity or NaN is involved, the result may also become Infinity or NaN.
console.log(20 - 10);
console.log('20' - '10');
console.log('20ee' - 10);
*) OperatorThe multiplication operator * multiplies two operands and returns their product.
20 * 10; // 200
'20' * '10'; // 200
'20ee' * 10; // NaN
If a string contains only a numeric value, JavaScript converts it to a number before multiplying. If the string is not numeric, the result becomes NaN.
Check the browser console (F12) to see the output.
console.log(20 * 10);
console.log('20' * '10');
console.log('20ee' * 10);
/) OperatorThe division operator / divides the left operand by the right operand and returns the quotient.
20 / 10; // 2
20 / -10; // -2
100 / 0; // Infinity
0 / 0; // NaN
This operator is useful for averages, ratios, percentages, and splitting values into equal parts.
Check the browser console (F12) to see the output.
console.log(20 / 10);
console.log('20' / '10');
console.log(100 / 0);
console.log(0 / 0);
%) OperatorThe modulus operator % returns the remainder after division.
5 % 3; // 2
20 % 9; // 2
20 % 10; // 0
This operator is very useful when you want to:
console.log(20 % 9); console.log(-20 % 9); console.log(20 % 10);
++) OperatorThe increment operator ++ increases the value of a variable by 1. It is a unary operator, so it works on only one operand.
There are two forms:
In prefix form, the value is increased first and then used.
let x = 10; let y = ++x; // x = 11, y = 11
In postfix form, the current value is used first and then increased.
let a = 10; let b = a++; // b = 10, a = 11
let x = 10; let y = ++x;let a = 10; let b = a++;
--) OperatorThe decrement operator -- decreases the value of a variable by 1. Like increment, it is also a unary operator.
let x = 10; let y = --x; // x = 9, y = 9
let a = 10; let b = a--; // b = 10, a = 9
let x = 10; let y = --x;let a = 10; let b = a--;
JavaScript arithmetic operators help you perform calculations on numbers and variables.
+ for addition and also for string concatenation.-, *, /, and % for mathematical calculations.++ and -- to increase or decrease a value by one.-, *, /, and %.+ because it can add numbers or join strings depending on the operands.Once you understand arithmetic operators well, writing calculations in JavaScript becomes much easier.
What is the result of the expression 10 % 3 in JavaScript?