JavaScript Math.trunc() Method

Definition
Math.trunc(x) cuts off the decimal part of a number and keeps only the whole number part. It does not round. Math.trunc(4.9) is 4.
Read the syntax below, then try the three examples to see the method in action.
Syntax
Call the method on Math. Start with this line.
| Call | What it does |
|---|
Math.trunc(x) | Returns the integer part of x. |
Built into JavaScript. Call Math.trunc(...) on the Math object — no import required. Math is not a constructor, so never write new Math().
Library declarations
Parameters
| Parameter | Description |
|---|
| x | Required. The number to cut. Other values are converted to a number first. |
Return Value
| Return Value | Description |
|---|
| Number | The integer part of x. Returns NaN if x is not a number. |
JavaScript Examples
Example 1 — Cut Off the Decimal Part
The digits after the decimal point are dropped.
console.log(Math.trunc(4.9)); // 4
console.log(Math.trunc(-4.9)); // -4
console.log(Math.trunc(0.99)); // 0
How It Works
- Positive.
4.9 becomes 4. It is not rounded up. - Negative.
-4.9 becomes -4, toward zero. - Less than one.
0.99 becomes 0. - Whole numbers. A number like
7 stays 7.
Example 2 — Compare with floor, ceil and round
Use -4.7 to see how each method behaves with negative numbers.
const n = -4.7;
console.log(Math.trunc(n)); // -4
console.log(Math.floor(n)); // -5
console.log(Math.ceil(n)); // -4
console.log(Math.round(n)); // -5
How It Works
- trunc. Cuts the decimal and moves toward zero, giving
-4. - floor. Always goes down, giving
-5. - ceil. Always goes up, giving
-4. - round. Goes to the nearest whole number, giving
-5.
Example 3 — Whole Part, Fraction Part and Special Inputs
Split a number into two parts, then try a few special inputs.
const value = 12.75;
const whole = Math.trunc(value);
const fraction = value - whole;
console.log(whole); // 12
console.log(fraction); // 0.75
console.log(Math.trunc("7.8")); // 7
console.log(Math.trunc(-0.5)); // -0
console.log(Math.trunc("abc")); // NaN
How It Works
- Whole part.
Math.trunc(12.75) is 12. - Fraction part. Subtract the whole part:
12.75 - 12 is 0.75. - Text number.
"7.8" is converted to a number first, so the result is 7. - Minus zero.
-0.5 becomes -0. - Bad text.
"abc" gives NaN.
Compatibility
Browser & Runtime Support
Math.trunc was added in ES2015 and is Baseline Widely available in modern browsers and Node.js. It does not work in Internet Explorer.
✓ Baseline · ES2015
Math.trunc
Supported in Chrome, Firefox, Safari, Edge and Node.js for years. Only very old browsers such as Internet Explorer lack it.
98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.trunc Excellent
Bottom line: Use Math.trunc in modern code. Add a small polyfill only if you must support Internet Explorer.
Frequently Asked Questions
It removes the decimal part of a number and returns only the whole number part. Math.trunc(4.9) is 4.
For positive numbers yes. For negative numbers no. Math.trunc(-4.7) is -4 but Math.floor(-4.7) is -5.
No. It simply drops the decimals. Use Math.round() if you want the nearest integer.
It returns NaN because the text cannot be converted to a number.
Subtract the truncated value: value - Math.trunc(value). For 12.75 that gives 0.75.
All modern browsers and Node.js. It was added in ES2015. Internet Explorer does not support it.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful