References

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

The JavaScript Math.trunc() method

Method JavaScript All modern browsers Updated
Quick answer

The Math.trunc() method removes the decimal part of a number, leaving the integer part — always toward zero. Math.trunc(4.9) is 4 and Math.trunc(-4.9) is -4. Unlike Math.floor(), it doesn't round down for negatives; it just chops off the fraction.

Overview

Math.trunc() simply cuts off everything after the decimal point. 4.9 becomes 4, 4.1 becomes 4, and the sign is preserved: -4.9 becomes -4. It doesn't round — it truncates, always toward zero.

That toward-zero behavior is what distinguishes it from Math.floor(). For positive numbers they agree, but for negatives they differ: Math.floor(-4.9) is -5 (rounds down, away from zero), while Math.trunc(-4.9) is -4 (just drops the fraction). When you want "the integer part" regardless of sign, trunc is the one you mean.

It rounds out the integer-conversion family: floor (down), ceil (up), round (nearest), and trunc (toward zero). An older trick people used for truncation was the double bitwise NOT (~~x) or x | 0, but those only work for 32-bit-range numbers and are less clear — Math.trunc() is the correct, readable choice.

Syntax

Math.trunc(4.9)    // 4
Math.trunc(4.1)    // 4
Math.trunc(-4.9)   // -4  (toward zero, not -5)
Math.trunc(-0.5)   // -0

Parameters

The Math.trunc() method accepts the following parameters.

Parameter Description
x The number to truncate to its integer part.

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const values = [4.9, -4.9, 7.01, -0.5];

  const lines = values.map(v =>
    'trunc(' + v + ') = ' + Math.trunc(v) +
    '  floor = ' + Math.floor(v)
  );

  document.getElementById('out').textContent = lines.join('\n');
</script>

Best practices

  • Use Math.trunc() to get the integer part of a number regardless of sign.
  • Remember it differs from Math.floor() only for negatives (toward zero vs down).
  • Prefer it over the old ~~x / x | 0 hacks — clearer and not limited to 32-bit range.
  • To separate the fraction, subtract: x - Math.trunc(x).

Frequently asked questions

What does Math.trunc() do?
It removes the fractional part of a number, returning the integer part toward zero. Math.trunc(4.7) is 4.
What is the difference between Math.trunc() and Math.floor()?
They agree on positives. For negatives, Math.floor() rounds down (-4.9-5) while Math.trunc() drops the fraction (-4.9-4).
How do I get just the integer part of a number?
Use Math.trunc(x) — it works for both positive and negative numbers.
Should I use ~~x to truncate?
No — that bitwise trick only handles 32-bit-range numbers and is unclear. Use Math.trunc().