Table of Content
JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the decimal part and keep only the whole number.
This helps when you want quick cuts without rounding. Developers use Math.trunc to chop float values cleanly and directly.
Understand JavaScript Math.trunc()
Math.trunc() removes the decimal part of a number. It keeps everything before the dot. It does not round, and it just slices off what comes after the decimal.
Here is the syntax:
Math.trunc(number)The function takes one number. It gives back the integer part. The original value stays the same.
JavaScript skips all rounding rules here. It does not care if the decimal is .1 or .9. It just drops it.
Here is a quick look:
Math.trunc(7.9)This gives you 7. It cuts off the .9 and keeps the whole number only.
Math.trunc(7.1)This still gives you 7. It just drops the .1 with no change to the base.
Here are use cases:
- Strip float values for fixed display.
- Force integer output without rounding.
- Remove decimal noise in calculations.
- Handle pixel values in layout math.
- Make score or value checks simple.
So, how does Math.trunc() really behave?
1 – It removes the decimal:
The function cuts the number after the dot. That is it. No checking. No changing.
2 – It works with negatives too:
Math.trunc keeps the sign. It still removes the decimal.
Math.trunc(-5.7) // -5
Math.trunc(-2.2) // -23 – No rounding, ever:
It does not matter if the decimal is .9 or .99999 — the function never moves the number up or down.
Examples of Math.trunc() in JavaScript
Cut float values:
Math.trunc(8.6) // 8
Math.trunc(3.3) // 3Handle negative numbers:
Math.trunc(-4.9) // -4
Math.trunc(-0.1) // 0Edge cases:
Math.trunc(0.9) // 0
Math.trunc(-0.9) // 0
Math.trunc(0) // 0 Zero and near-zero values behave as expected. The decimal vanishes. The sign stays if it exists.
Browser Support for Math.trunc()
You can use Math.trunc() in most modern browsers. It works out of the box in recent versions. You get solid support across desktop and mobile.
Here is a list of supported browsers:
- Google Chrome supports it from version 38.
- Mozilla Firefox supports it from version 25.
- Microsoft Edge supports it from version 12.
- Apple Safari supports it from version 8.
- Opera includes support from version 25.
- Internet Explorer does not support Math.trunc().
- Mobile browsers like Chrome for Android and Safari on iOS support it.
If you work in older environments like IE, you may need a fallback.
Wrapping Up
In this article, you learned how JavaScript Math.trunc() works. You also saw how it handles decimals, negatives, and special edge values.
Here is a quick recap:
- Math.trunc() removes the decimal part of a number.
- It does not round. It just chops.
- The function gives back only the whole number.
- It keeps the sign of the number.
- Math.trunc(7.9) gives 7. Math.trunc(7.1) also gives 7.
- Math.trunc(-4.9) gives -4. Math.trunc(-0.9) gives 0.
- Use it when you want no rounding — just straight-up cutting.
- All modern browsers support it, except Internet Explorer.
What does Math.trunc() do with decimal numbers?
How does Math.trunc() handle negative numbers?
Is Math.trunc() supported in all browsers?
Similar Reads
JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps…
Arrays often hold other arrays. This happens with API responses, form data, or nested objects. These layers add extra steps…
Object References in JavaScript mean that variables do not store full objects but only their references in memory. What is…
Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…
The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…
Math.asin() finds the arc sine of a number in JavaScript. It helps you work with angles from values. You get…
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…
Data types in JavaScript help hold values and shape code rules. They set clear plans for text, numbers, and other…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
JavaScript gives you several ways to round numbers, but not all of them round the same way. If you want…