JavaScript Arithmetic Operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and more. These operators work with numbers.
Example:
let sum = 10 + 20; console.log(sum); // 30
List of Arithmetic Operators:
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 3 |
1.67 |
% |
Modulus (remainder) | 5 % 3 |
2 |
** |
Exponentiation (power) | 5 ** 3 |
125 |
++ |
Increment | let x = 5; x++ |
6 |
-- |
Decrement | let x = 5; x-- |
4 |
1. Addition (+
)
Adds two values.
Example:
let sum = 8 + 12; console.log(sum); // 20
2. Subtraction (-
)
Subtracts the right-hand operand from the left-hand operand.
Example:
let difference = 20 - 5; console.log(difference); // 15
3. Multiplication (*
)
Multiplies two values.
Example:
let product = 4 * 5; console.log(product); // 20
4. Division (/
)
Divides the left-hand operand by the right-hand operand.
Example:
let quotient = 20 / 4; console.log(quotient); // 5
5. Modulus (%
)
Returns the remainder of the division.
Example:
let remainder = 10 % 3; console.log(remainder); // 1
6. Exponentiation (**
)
Raises the first operand to the power of the second operand.
Example:
let power = 2 ** 3; console.log(power); // 8
7. Increment (++
)
Increases a variable’s value by 1.
- Postfix (
x++
): Returns the value before incrementing. - Prefix (
++x
): Returns the value after incrementing.
Example:
let x = 5; console.log(x++); // 5 (returns 5, then increments to 6) console.log(++x); // 7 (increments to 7, then returns 7)
8. Decrement (--
)
Decreases a variable’s value by 1.
- Postfix (
x--
): Returns the value before decrementing. - Prefix (
--x
): Returns the value after decrementing.
Example:
let y = 5; console.log(y--); // 5 (returns 5, then decrements to 4) console.log(--y); // 3 (decrements to 3, then returns 3)
Summary
Arithmetic operators are fundamental in JavaScript for performing basic math operations. Understanding how each operator works will help in performing calculations and manipulating data effectively in your code.