Math.trunc()

Math.trunc(x)
Returns: number · Added in vES6 · Updated March 13, 2026 · Math
math trunc truncate integer numbers

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

ParameterTypeDefaultDescription
xnumberThe number to truncate

Return Value

The integer portion of x, removing any fractional digits. The sign is preserved.

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

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.

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
  • Clarity: x | 0 looks like a type cast hack
  • Consistency: Bitwise operations convert to 32-bit integers, which can cause unexpected results with large numbers
// 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

Expression3.7-3.73.2-3.2
Math.trunc(x)3-33-3
Math.floor(x)3-43-4
Math.ceil(x)4-34-3
Math.round(x)4-43-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

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