JavaScript Math.trunc() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
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.

JavaScript
Math.trunc(x)
CallWhat 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
JavaScript
Math.trunc(x)

Parameters

ParameterDescription
xRequired. The number to cut. Other values are converted to a number first.

Return Value

Return ValueDescription
NumberThe 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.

JavaScript
console.log(Math.trunc(4.9)); // 4
console.log(Math.trunc(-4.9)); // -4
console.log(Math.trunc(0.99)); // 0
Try It Yourself

How It Works

  1. Positive. 4.9 becomes 4. It is not rounded up.
  2. Negative. -4.9 becomes -4, toward zero.
  3. Less than one. 0.99 becomes 0.
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. trunc. Cuts the decimal and moves toward zero, giving -4.
  2. floor. Always goes down, giving -5.
  3. ceil. Always goes up, giving -4.
  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.

JavaScript
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
Try It Yourself

How It Works

  1. Whole part. Math.trunc(12.75) is 12.
  2. Fraction part. Subtract the whole part: 12.75 - 12 is 0.75.
  3. Text number. "7.8" is converted to a number first, so the result is 7.
  4. Minus zero. -0.5 becomes -0.
  5. Bad text. "abc" gives NaN.

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 support
Math.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.

Next: E

Learn about Euler's number, about 2.718.

E tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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