References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Division / operator

Operator JavaScript All modern browsers Updated
Quick answer

The division operator / divides the left number by the right: 10 / 4 is 2.5. JavaScript division always produces a decimal (no separate integer division) — use Math.floor() or Math.trunc() for whole numbers. Dividing by zero gives Infinity (not an error), and 0 / 0 gives NaN.

Overview

/ divides one number by another, and unlike some languages it always gives a floating-point result: 10 / 4 is 2.5, not 2. There's no built-in integer division, so when you want a whole number you combine it with rounding — Math.floor(a / b) or Math.trunc(a / b) — and for the leftover you use the remainder operator %. Like the other arithmetic operators, it coerces string operands to numbers.

The behavior people don't expect is dividing by zero. In JavaScript it doesn't throw — 5 / 0 is Infinity, -5 / 0 is -Infinity, and the indeterminate 0 / 0 is NaN. So division never crashes, but it can quietly produce Infinity or NaN that propagate through later math. Guard divisors that might be zero, and check results with Number.isFinite() when input is uncertain.

It shares precedence with multiplication (both before +/-), evaluated left to right. There's a compound form /= that divides and reassigns. And, as ever with floating point, exact decimals aren't guaranteed — round results for display.

Syntax

a / b

10 / 4       // 2.5
Math.floor(10 / 4)  // 2  (integer division)
5 / 0        // Infinity (not an error)
0 / 0        // NaN
x /= 2;      // divide and reassign

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const total = 17, perGroup = 5;

  document.getElementById('out').textContent =
    'exact:  ' + (total / perGroup) + '\n' +
    'groups: ' + Math.floor(total / perGroup) + '\n' +
    '5 / 0:  ' + (5 / 0); // 3.4 / 3 / Infinity
</script>

Best practices

  • Use Math.floor() or Math.trunc() for integer division — / always returns a decimal.
  • Guard divisors that could be zero — division gives Infinity or NaN instead of throwing.
  • Validate results with Number.isFinite() when input is uncertain.
  • Use the remainder % for the leftover after division.

Frequently asked questions

What happens when you divide by zero in JavaScript?
It doesn't error — 5 / 0 is Infinity, -5 / 0 is -Infinity, and 0 / 0 is NaN.
How do I do integer division?
JavaScript has no integer division — use Math.floor(a / b) or Math.trunc(a / b) to drop the decimal.
Why is my division result a long decimal?
Floating-point math can't represent some results exactly. Round with toFixed() or Math.round() for display.
What does /= do?
It divides and reassigns: x /= 2 is shorthand for x = x / 2.