3.2 Arithmétic Operators
The first family of operators is the arithmetic family. As in mathematics, these operators perform calculations. They usually take two operands and produce a new value. The combination of operands and operators is called an expression, and an expression always produces a value.
Let's declare two variables let x = 10; let y = 3; and review every arithmetic operator available in JavaScript:
console.log(x + y); // addition
console.log(x - y); // subtraction
console.log(x * y); // multiplication
console.log(x / y); // division
console.log(x % y); // remainder of x divided by y
console.log(x ** y); // exponentiation: x to the power of y
The exponentiation operator ** is the JavaScript-specific addition to the classic math set. All the others should look familiar.
Increment and decrement
JavaScript provides two more fundamental arithmetic operators: ++ (increment by 1) and -- (decrement by 1). Their behavior depends on whether they are placed before or after the variable:
- Prefix
++x: first incrementx, then return its new value. Logging it prints 11. - Postfix
x++: first return the current value ofx, then increment. Logging it prints 10, and the next access prints 11. --xandx--behave the same way but subtract 1 instead of adding.
To recap: addition, subtraction, multiplication, division, remainder, exponentiation, and the increment/decrement operators form the complete arithmetic toolbox in JavaScript. In the next video, we'll move on to assignment operators.
Summary
This lesson covers the fundamental arithmetic operators in JavaScript used to perform mathematical calculations. Students learn the basic operators (addition, subtraction, multiplication, division, modulo, and exponentiation) and discover two specialized unary operators—increment (++) and decrement (--) with both prefix and postfix variations. The distinction between pre and post operations is crucial, as they produce different results depending on whether the value is modified before or after being displayed.
Key points
- Arithmetic operators in JavaScript allow mathematical calculations on numeric values and produce new values
- The exponentiation operator (represented by ** in JavaScript) raises a number to the power of another number
- Pre-increment (++x) modifies the value first then displays it, while post-increment (x++) displays first then modifies
- The modulo operator (%) returns the remainder after division
- Decrement operators (--) work similarly to increment operators with the same prefix/postfix distinction
- All arithmetic operators help build expressions—code structures that produce values
FAQ
What is the difference between ++x and x++ in JavaScript?
++x (pre-increment) increments the variable first and then returns the new value, while x++ (post-increment) returns the current value first and then increments the variable. This distinction matters when the result is used immediately in expressions.
How does the modulo operator work in arithmetic operations?
The modulo operator (%) returns the remainder after division. For example, if you divide one number by another, the modulo operator gives you what is left over after the division is complete.
What operator in JavaScript is used for exponentiation?
In JavaScript, exponentiation is represented by two asterisks (**). To raise x to the power of y, you write x ** y, following the same mathematical principle as exponentiation in standard mathematics.