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.
Table of Content
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) // -2
3 – 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) // 3
Handle negative numbers:
Math.trunc(-4.9) // -4
Math.trunc(-0.1) // 0
Edge 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 Math.log() was added to help people find the natural log of a number. It solves real problems in science…
Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…
Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…
The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
The ternary operator in JavaScript keeps your code short. It helps you write conditions inside one line. JavaScript lets you…
You will face hoisting early when you write JavaScript. It affects how your code runs, and it may confuse you…
You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…