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
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…
If you are ready to take your first steps into the realm of web development, then there cannot be a…
JavaScript syntax is what you will use when writing and reading javascript code, it is the foundation of using this…
Arrow function gives you short code and keeps this in the right scope in JavaScript. Understand the Arrow Function in…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…
Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…
You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…
The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…
Mocha is a JavaScript test framework for Node.js. It runs tests in sequence and shows results. It works with any…