Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Arithmetic Operators

Arithmetic operators handle the math in JavaScript: addition, subtraction, multiplication, division, modulo, and exponentiation.

They are essential for anything from totals in a cart to time calculations and animation math.

Why We Need It

Nearly every real program performs math. If you misunderstand arithmetic operators, you can create subtle logic and precision bugs.

Knowing how these operators behave with strings, floats, and edge cases keeps calculations reliable.

Syntax

a + b   // addition
a - b   // subtraction
a * b   // multiplication
a / b   // division
a % b   // remainder
a ** b  // exponentiation
a++     // increment (postfix)
++a     // increment (prefix)
a--     // decrement (postfix)
--a     // decrement (prefix)

Basic Example

1. Basic Arithmetic

console.log(5 + 3);   // 8
console.log(10 - 4);  // 6
console.log(3 * 7);   // 21
console.log(15 / 3);  // 5
console.log(17 % 5);  // 2

Addition, subtraction, multiplication, division, and modulo are the core arithmetic tools.

Real World Example

2. Exponentiation

console.log(2 ** 3);      // 8
console.log(16 ** 0.5);   // 4 (square root)
console.log(2 ** 3 ** 2); // 512 (right-associative)

Use `**` for powers and fractional exponents for roots.

Multiple Use Cases

Everyday Calculations

Arithmetic operators power totals, averages, discounts, and time calculations. You use them for almost any numeric work in JavaScript.

The same operators also work with floating-point numbers, so be mindful of precision in money or measurement logic.

  • Use `+` for addition and `-` for subtraction.
  • Use `*` and `/` for multiplication and division.
  • Use `%` for remainders in cycles or indexing.

Exponentiation

The `**` operator makes power calculations clear and readable. It is right-associative, so `2 ** 3 ** 2` means `2 ** (3 ** 2)`.

For roots, use fractional exponents like `16 ** 0.5` for square roots.

Increment and Decrement

`++` and `--` update a variable by one. Prefix changes first, postfix changes after the expression returns.

These operators are handy in loops but can hurt readability in complex expressions.

Floating Point Pitfalls

JavaScript uses IEEE 754 floating-point numbers. That means `0.1 + 0.2` is not exactly `0.3`.

When precision matters, compare using a tolerance (like `Number.EPSILON`) or work with integers (cents instead of dollars).

More Examples

3. Increment vs Decrement

let x = 5;
console.log(x++); // 5 (postfix)
console.log(x);   // 6
console.log(++x); // 7 (prefix)
console.log(--x); // 6

Prefix changes the value first, postfix returns first and then changes it.

4. Precision Check

const sum = 0.1 + 0.2;
console.log(sum); // 0.30000000000000004

const closeEnough = Math.abs(sum - 0.3) < Number.EPSILON;
console.log(closeEnough); // true

Floating-point arithmetic can be imprecise, so compare with a tolerance.

Comparison

Without

// Without compound operators
let total = 10;
total = total + 5;

// Using Math.pow
const squared = Math.pow(4, 2);

With

// With arithmetic operators
let total = 10;
total += 5;

const squared = 4 ** 2;

Common Mistakes and Fixes

Treating 0.1 + 0.2 as exact

Use a tolerance for comparisons or work with integers for money.

Mixing strings and numbers

Convert explicitly with Number() to avoid accidental concatenation.

Confusing prefix and postfix

Avoid ++/-- in complex expressions for readability.

Division by zero assumptions

JavaScript returns Infinity, so guard before dividing.

Interview Questions

What does the modulo operator do?

It returns the remainder after division, often used for cycles or parity.

Why is 0.1 + 0.2 imprecise?

Binary floating-point cannot exactly represent some decimals.

What is the difference between prefix and postfix increment?

Prefix increments before returning the value; postfix returns first, then increments.

Practice Problem

Practice: Calculate a discounted price and tax. Use arithmetic operators and print the final amount.

const price = 120;
const discount = 0.2;
const taxRate = 0.18;
// TODO: compute final amount

One Possible Solution

const price = 120;
const discount = 0.2;
const taxRate = 0.18;
const afterDiscount = price - price * discount;
const final = afterDiscount + afterDiscount * taxRate;
console.log(final);

Frequently Asked Questions

Why does 0.1 + 0.2 not equal 0.3?

Floating-point numbers cannot represent some decimals exactly. Use a tolerance or integer math for precision-sensitive work.

Is ** the same as Math.pow?

Yes. `a ** b` is equivalent to `Math.pow(a, b)` but more readable.

What does % do in JavaScript?

It returns the remainder after division, useful for cycles, parity checks, and wrapping indexes.

Should I avoid ++ and --?

They are fine in loops, but can reduce clarity in complex expressions.

Try It Yourself

Run the arithmetic examples and change the values to see how each operator behaves.