JavaScript Arithmetic Operators

Arithmetic operators perform mathematical calculations on numbers. These operators enable you to add, subtract, multiply, divide, and perform other mathematical operations essential for calculations, counters, and data processing in your programs.

Basic Arithmetic

The addition operator (+) adds two numbers together. When used with numbers, it performs mathematical addition (5 + 3 = 8). However, when used with strings, the + operator concatenates (joins) them together ("Hello" + "World" = "HelloWorld"). If you add a number to a string, JavaScript converts the number to a string and concatenates them.

The subtraction operator (-) subtracts the second number from the first. Unlike addition, subtraction only works with numbers and will attempt to convert strings to numbers. If the conversion fails (like "hello" - 5), the result is NaN (Not a Number), a special value indicating an invalid numeric operation.

The multiplication operator (*) multiplies two numbers. Like subtraction, multiplication attempts to convert strings to numbers if necessary. The operator follows standard mathematical rules, including the order of operations (multiplication before addition/subtraction). Use parentheses to control the order of complex calculations.

The division operator (/) divides the first number by the second. Division by zero in JavaScript doesn't throw an error—it returns Infinity (or -Infinity for negative dividends). Division always returns a floating-point result, even when dividing two integers that divide evenly (though 10 / 2 = 5, not 5.0 in display).

The modulus operator (%) returns the remainder after division. For example, 10 % 3 = 1 because 10 divided by 3 is 3 with remainder 1. Modulus is extremely useful for checking if numbers are even/odd (n % 2 === 0 for even), cycling through arrays, or wrapping values within a range.

The exponentiation operator (**) raises the first number to the power of the second. For example, 2 ** 3 = 8 (2 cubed). This operator was added in ES2016 (ES7) as a more readable alternative to Math.pow(). It's particularly useful for scientific calculations, compound interest, and geometric progressions.

// Basic operations
let a = 10;
let b = 3;

let sum = a + b;      // 13
let diff = a - b;     // 7
let product = a * b;  // 30
let quotient = a / b; // 3.333...
let remainder = a % b; // 1
let power = a ** b;   // 1000

// String concatenation
let str = "Hello" + " " + "World"; // "Hello World"
let mixed = "Number: " + 42; // "Number: 42"

Increment and Decrement

The increment operator (++) increases a variable's value by 1. It's a shorthand for "variable = variable + 1". So x++ is equivalent to x = x + 1. This operator is one of the most commonly used in programming, especially in loops where you need to count iterations or track position in a sequence.

The decrement operator (--) decreases a variable's value by 1. Like increment, it's shorthand for "variable = variable - 1". So x-- is equivalent to x = x - 1. Decrement is useful for counting down, processing lists in reverse, or any situation where you need to reduce a counter by one.

Both increment and decrement can be used in two positions: prefix (++x or --x) and postfix (x++ or x--). The position matters when the operator is used in an expression. Prefix increments/decrements the value first, then returns the new value. Postfix returns the current value first, then increments/decrements. When used alone (not in an expression), both positions have the same effect.

Prefix increment (++x) means "increment first, then use the value". If you write "let b = ++a" where a is 5, JavaScript increments a to 6 first, then assigns 6 to b. Both a and b end up as 6. The increment happens before the value is used in the expression.

Postfix increment (x++) means "use the value first, then increment". If you write "let b = a++" where a is 5, JavaScript assigns the current value of a (5) to b first, then increments a to 6. So b is 5 and a is 6. The increment happens after the value is used in the expression. This subtle difference is important when using increment/decrement in assignments or complex expressions.

Increment and decrement operators are most commonly used in for loops to count iterations: "for (let i = 0; i < 10; i++)". They're also used in while loops, array indexing, and any situation where you need a counter. In modern JavaScript, for...of loops often eliminate the need for manual counter management, but increment/decrement remain essential tools.

// Increment
let x = 5;
x++;        // x is now 6
++x;        // x is now 7

// Decrement
let y = 5;
y--;        // y is now 4
--y;        // y is now 3

// Prefix vs Postfix
let a = 5;
let b = ++a;  // a=6, b=6 (increment first)

let c = 5;
let d = c++;  // c=6, d=5 (assign first)