Math.trunc()
Removes the fractional digits from a number, truncating toward zero.
Math.trunc(x)- Added in JavaScript ES2015
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number to truncate. Non-numbers are coerced first. |
Return value
numberThe integer part of x, with all fractional digits removed. Truncation is always toward zero.
Math.trunc() deletes everything after the decimal point and leaves the sign alone. 13.37 becomes 13; -13.37 becomes -13. Unlike Math.floor() and Math.ceil(), it does not care about direction on the number line — it always moves toward zero.
When to reach for it
Use it when you literally mean "the integer part": splitting a value into whole and fractional components, implementing integer division that behaves the way C or Java would, or normalising a numeric input that may carry stray decimals. It is the correct choice whenever a negative value should keep the same integer digits it had.
What trips people up
Truncation is not rounding. Math.trunc(4.99) is 4, which surprises people who reached for it to "clean up" a value. If you want nearest, use Math.round().
It also preserves negative zero: Math.trunc(-0.123) returns -0, which compares equal to 0 but shows its sign in console.log and flips the sign of a subsequent division.
Math.trunc() is ES2015, and it is the safe replacement for the older ~~x and x | 0 tricks. Those truncate too, but only within signed 32-bit range — ~~3000000000.5 wraps to a negative number while Math.trunc() returns 3000000000.
Example
When to use it
- Splitting a decimal into its whole-number and fractional halves
- Implementing integer division that truncates toward zero for negative operands
- Stripping stray decimals from a numeric input before using it as a count
- Replacing ~~x or x | 0 where the value can exceed 32-bit range
Frequently asked questions
What is the difference between Math.trunc() and Math.floor()?
Math.trunc() always moves toward zero while Math.floor() always moves toward negative infinity. They agree on positive numbers, but Math.trunc(-1.5) is -1 and Math.floor(-1.5) is -2.
Does Math.trunc() round the number?
No — it discards the fractional digits regardless of their size, so Math.trunc(4.99) is 4. Use Math.round() if you want the nearest integer.
Is Math.trunc() the same as ~~x?
Only for values that fit in a signed 32-bit integer. Beyond about 2.1 billion the bitwise form wraps to a wrong value, and it also coerces non-numbers differently, so prefer Math.trunc().
More examples
Splitting whole and fractional parts
Because trunc moves toward zero, the fractional remainder always carries the same sign as the input and its magnitude stays below 1. Using Math.floor here would give -8 and 0.75 for -7.25, which is correct arithmetic but usually not what a display wants.
function split(n) {
const whole = Math.trunc(n);
return { whole, fraction: n - whole };
}
console.log(split(7.25)); // { whole: 7, fraction: 0.25 }
console.log(split(-7.25)); // { whole: -7, fraction: -0.25 }
console.log(split(3)); // { whole: 3, fraction: 0 }Safe for large numbers, unlike bitwise truncation
The bitwise operators coerce their operand to a signed 32-bit integer first, so any value above 2147483647 wraps around to a wrong, often negative, result. Math.trunc has the full double range and passes Infinity through unchanged.
const big = 3_000_000_000.75;
console.log(Math.trunc(big)); // 3000000000
console.log(~~big); // -1294967296 — wrapped to 32 bits
console.log(big | 0); // -1294967296
console.log(Math.trunc(Number.MAX_SAFE_INTEGER + 0.5));
console.log(Math.trunc(Infinity)); // Infinity
Discussion