JavaScript arithmetic involves performing mathematical operations using operators.
Basic arithmetic operations include addition (+
), subtraction (-
), multiplication (*
), division (/
), and modulus (%
, which gives the remainder of a division).
JavaScript also supports the increment (++
) and decrement (--
) operators to adjust values by one.
Additionally, compound assignment operators like +=
, -=
, *=
, and /=
combine an arithmetic operation with assignment, streamlining code for updating variables.
Here's a quick breakdown:
+
(Addition): Adds two values.-
(Subtraction): Subtracts one value from another.*
(Multiplication): Multiplies two values./
(Division): Divides one value by another.%
(Modulus): Returns the remainder of a division.
Examples
5 + 3
// 810 - 2
// 84 * 7
// 2820 / 4
// 515 % 4
// 3let x = 5; x++
// 6let y = 8; y--
// 7let a = 10; a += 5
// 15let b = 6; b *= 2
// 12let c = 30; c /= 5
// 6
let addition = 5 + 3;
console.log("Addition: 5 + 3 =", addition);
let subtraction = 10 - 2;
console.log("Subtraction: 10 - 2 =", subtraction);
let multiplication = 4 * 7;
console.log("Multiplication: 4 * 7 =", multiplication);
let division = 20 / 4;
console.log("Division: 20 / 4 =", division);
let modulus = 15 % 4;
console.log("Modulus: 15 % 4 =", modulus);
let x = 5;
x++;
console.log("Increment: x++ =", x);
let y = 8;
y--;
console.log("Decrement: y-- =", y);
let a = 10;
a += 5;
console.log("Compound Assignment (Addition): a += 5 =", a);
let b = 6;
b *= 2;
console.log("Compound Assignment (Multiplication): b *= 2 =", b);
let c = 30;
c /= 5;
console.log("Compound Assignment (Division): c /= 5 =", c);