JavaScript Math.trunc(): Remove Fractional Digits

Tech reviewed: Deepak Prasad
JavaScript Math.trunc(): Remove Fractional Digits

JavaScript Math.trunc() strips the fractional part of a number and returns the integer portion, always moving toward zero (unlike Math.floor, which moves toward negative infinity on negatives). Use it when you want predictable truncation without implicit string coercion. For flooring toward negative infinity specifically, compare with JavaScript Math.floor.

Tested On: The examples were tested with Node.js v20.18.1 on a Linux system. The same Math.trunc() behavior works in modern browsers and JavaScript runtimes.


Math.trunc Syntax

Number line comparing Math.trunc toward zero versus Math.floor for negative fractions

javascript
Math.trunc(value)

Math.trunc() removes digits after the decimal point. It does not round up or down by value; it moves toward zero.


Method 1: Truncate a Positive Number

javascript
console.log(Math.trunc(13.89));

Output:

text
13

Method 2: Truncate a Negative Number

javascript
console.log(Math.trunc(-13.89));

Output:

text
-13

This is why Math.trunc() differs from Math.floor() for negative numbers.


Method 3: Compare Math.trunc() and Math.floor()

javascript
console.log(Math.trunc(-3.9));
console.log(Math.floor(-3.9));

Output:

text
-3
-4

Math.trunc() removes the fraction. Math.floor() rounds down toward negative infinity.


Method 4: Handle Invalid Values

javascript
console.log(Math.trunc("hello"));

Output:

text
NaN

Common Questions About Math.trunc

What does Math.trunc do?

It removes fractional digits and returns the integer part of a number.

Does Math.trunc round toward zero?

Yes. Math.trunc(3.9) returns 3, and Math.trunc(-3.9) returns -3.

How is Math.trunc different from Math.floor?

For positive numbers they often match. For negative numbers, Math.floor(-3.9) returns -4, while Math.trunc(-3.9) returns -3.


Summary

JavaScript Math.trunc() is the built-in way to truncate a number by removing fractional digits. Use it when you need the integer part of a value and want rounding toward zero. It is especially useful for integer division and numeric cleanup where Math.floor() would be wrong for negative numbers.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design