Math.trunc()
Math.trunc(x) Math.trunc() removes the fractional part of a number, returning only the integer portion. Unlike rounding, truncation simply discards everything after the decimal point—regardless of whether the number is positive or negative.
Syntax
Math.trunc(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | number | — | The number to truncate |
Return Value
The integer portion of x, removing any fractional digits. The sign is preserved. Unlike Math.floor(), truncation always moves toward zero — it’s the simplest rounding operation because it doesn’t care about magnitude, only about dropping everything after the decimal.
Examples
Basic truncation
// Positive numbers: trunc drops decimals just like floor
Math.trunc(4.9); // 4
Math.trunc(4.1); // 4
Math.trunc(0.999); // 0
// Negative numbers: trunc rounds toward zero (unlike floor!)
Math.trunc(-4.9); // -4
Math.trunc(-4.1); // -4
// Whole numbers unchanged
Math.trunc(42); // 42
Math.trunc(-42); // -42
Comparing trunc vs floor with negative numbers
Negative numbers are where Math.trunc() pulls its weight. Both trunc and floor behave identically for positive values, but they diverge sharply the moment the input goes below zero. The result can differ by a full integer, which is enough to break a calculation. This is the critical distinction that makes Math.trunc() essential:
const value = -3.7;
Math.trunc(value); // -3 (toward zero)
Math.floor(value); // -4 (toward negative infinity)
Math.ceil(value); // -3 (toward positive infinity)
Math.round(value); // -4 (nearest integer)
// Practical example: calculating remaining days
const daysRemaining = -2.5;
Math.trunc(daysRemaining); // -2 (not -3!)
When dealing with negative values — coordinates, temperature changes, financial adjustments — truncation toward zero is often the intuitive choice. Before Math.trunc() was added in ES6, developers had to reach for bitwise tricks to get the same effect. Those hacks still work, but they carry sharp edges.
Bitwise alternatives
Before Math.trunc() existed (ES6), developers used bitwise hacks:
const x = 3.7;
const y = -3.7;
// Bitwise OR with zero
x | 0; // 3
y | 0; // -3
// Double bitwise NOT (faster, but obscure)
~~x; // 3
~~y; // -3
// Same result as Math.trunc()
Math.trunc(x); // 3
Math.trunc(y); // -3
Why avoid bitwise tricks?
- Readability:
Math.trunc(x)is self-documenting, while~~xmakes newcomers consult a search engine - Clarity:
x | 0looks like a type-cast hack, not a truncation - Consistency: Bitwise operations convert to 32-bit integers, which can cause unexpected results with large numbers. The example below demonstrates exactly how this goes wrong.
// The bitwise trap with large numbers
const large = 2**31 + 0.5; // 2147483648.5
Math.trunc(large); // 2147483648
large | 0; // 0 (wraps around 32-bit!)
trunc vs floor vs ceil vs round
| Expression | 3.7 | -3.7 | 3.2 | -3.2 |
|---|---|---|---|---|
Math.trunc(x) | 3 | -3 | 3 | -3 |
Math.floor(x) | 3 | -4 | 3 | -4 |
Math.ceil(x) | 4 | -3 | 4 | -3 |
Math.round(x) | 4 | -4 | 3 | -3 |
When to use each:
- trunc: Drop decimals (direction toward zero)
- floor: Always round down (toward -∞)
- ceil: Always round up (toward +∞)
- round: Nearest integer (.5 rounds up)
Edge Cases
Math.trunc(NaN); // NaN
Math.trunc(Infinity); // Infinity
Math.trunc(-Infinity); // -Infinity
Math.trunc(0); // 0
Math.trunc(-0); // -0
Math.trunc(null); // 0 (coerced!)
Math.trunc(undefined); // NaN
When truncation is the right choice
Math.trunc() is useful when you want to drop fractional digits without rounding up or down. That makes it a good fit for coordinates, offsets, counters, and any calculation where the integer part matters more than the remainder. The sign stays intact, so negative values move toward zero instead of toward negative infinity.
This is different from Math.floor(), which changes the result for negative inputs. If you are working with values that can cross zero, the distinction matters a lot. Math.trunc() gives you a predictable “just remove the decimals” rule.
Input coercion and edge cases
One thing to remember is that Math.trunc() still follows JavaScript coercion rules. Strings can become numbers, null becomes 0, and undefined becomes NaN. That can be helpful in quick scripts, but it can also hide bad input if you expected a strict number.
If the value comes from a form or API, validate it before truncating. That keeps the function focused on formatting the number you already trust rather than doubling as a validator.
Performance Note
Bitwise operations (~~x or x | 0) are marginally faster on older engines, but the difference is negligible in real applications. Use Math.trunc() for clarity—it’s semantically correct and immediately understandable to anyone reading your code.
See Also
- Math.floor() — Round down
- Math.ceil() — Round up
- Math.round() — Round to nearest
- Number.isInteger() — Check if a value is an integer