Math.trunc
Returns the integer part of a number by removing any fractional digits
Syntax
Math.trunc(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | A number |
Return Value
The integer part of x
Examples
console.log(Math.trunc(13.37)); // 13
console.log(Math.trunc(-0.123)); // -0
console.log(Math.trunc(42)); // 42console.log(Math.trunc(-4.7)); // -4
console.log(Math.floor(-4.7)); // -5 // Note the differenceconst pixels = [100.5, 200.8, 50.3];
const integers = pixels.map(Math.trunc);
console.log(integers); // [100, 200, 50]Understanding Math.trunc
The Math.trunc method in JavaScript returns the integer part of a number by removing any fractional digits. It belongs to the Math object and is one of the most widely used methods for working with math values in modern JavaScript and TypeScript applications.
The method signature is Math.trunc(x). It accepts 1 parameter: x. When called, it returns the integer part of x. Understanding when and how to use trunc() helps you write more expressive, readable code.
Common use cases for Math.trunc include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like math-floor, math-ceil, math-round, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Math.trunc is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Math Methods
Other methods in the Math object
Related Tools
More Math Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.