Math.trunc JavaScript | Syntax, Parameter, Return Value & Usage

One of the functions of Javascript is Math.trunc, which allows developers to truncate numerical values to their integer part.

By the end of this article, we will know what is math.trunc function, its syntax, parameters, return value, and how to use it in your code.

Besides that, we will also cover factors why we should use this function and its difference from other rounding functions such as math.floor and math.ciel.

What is Math trunc in JavaScript?

Math.trunc is a built-in function in JavaScript that removes the decimal part of a given number and returns only the integer component. It effectively truncates the number towards zero, without rounding.

The primary advantage of using math.trunc is that it offers a faster alternative to the Math.floor or Math.ceil methods, which require more computational resources.

Syntax:

Math.trunc(number);

Parameter:

  • number (required): The numeric value that you want to truncate to its integer part.

Return Value:

  • The math.trunc() method returns the truncated integer part of the given number.

Note: The math.trunc() function does not perform any rounding; it simply discards the decimal part, effectively truncating towards zero.

How to use Math trunc in Javascript?

To use Math trunc in Javascript, you need to understand the syntax and functionality of the trunc() method.

The trunc() method takes a single argument, which is the number you want to truncate. Basically, it returns the integer part of the provided number without rounding.

let’s use the syntax of the trunc() method:

Math.trunc(number);

Now, explore these examples to see how they work.

Example 1: Basic Truncation

const sampleNumber = 3.14159;
const truncatedNumber = Math.trunc(sampleNumber);
console.log(truncatedNumber); // Output: 3

In this example, the trunc() method removes the decimal part of the number, resulting in the value 3.

Example 2: Truncating Negative Numbers

const sampleNegativeNumber = -7.987;
const truncatedNegativeNumber = Math.trunc(sampleNegativeNumber);
console.log(truncatedNegativeNumber); // Output: -7

The trunc() method works seamlessly with negative numbers as well, preserving the sign and removing the decimal part.

Example 3: Truncating Integers

const sampleInteger = 77;
const truncatedInteger = Math.trunc(sampleInteger);
console.log(truncatedInteger); // Output: 77

When you pass an integer to the trunc() method, it remains unchanged, as there is no decimal part to truncate.

Example 4: Truncating NaN (Not a Number)

const sampleNanValue = NaN;
const truncatedNaN = Math.trunc(sampleNanValue);
console.log(truncatedNaN); // Output: NaN

When the input is NaN, the trunc() method returns NaN as well.

Example 5: Truncating Infinity

const sampleInfinityValue = Infinity;
const truncatedInfinity = Math.trunc(sampleInfinityValue);
console.log(truncatedInfinity); // Output: Infinity

Similarly, when the input is positive or negative Infinity, the trunc() method returns the same Infinity value.

Example 6: Using Trunc in a Function

function customTruncate(number) {
  return Math.trunc(number);
}

const output = customTruncate(9.99);
console.log(output); // Output: 9

You can encapsulate the trunc() method in a custom function for reusability and clarity.

What is the difference between Math floor and Math trunc in JavaScript?

While Math.trunc removes the decimal portion without rounding, Math.floor always rounds down to the nearest integer, and Math.ceil rounds up to the nearest integer.

Let’s illustrate the differences with examples:

Math.trunc:

console.log(Math.trunc(4.8)); // Output: 4
console.log(Math.trunc(-2.2)); // Output: -2

Math.floor:

console.log(Math.floor(4.8)); // Output: 4
console.log(Math.floor(-4.2)); // Output: -5

Math.ceil

console.log(Math.ceil(3.8)); // Output: 4
console.log(Math.ceil(-3.1)); // Output: -3

Why Should You Use Math trunc in Javascript?

After finding how to use it, here are additional factors why using the Math.trunc() method in Javascript can offer:

  1. Truncate Numbers: It allows you to quickly and efficiently truncate decimal numbers to integers without any rounding, which is crucial for certain mathematical calculations.
  2. Performance: Compared to other rounding methods, Math.trunc() is faster and more straightforward since it does not perform any rounding operations.
  3. Readability: When working with data that requires precision, truncation provides clarity and avoids confusion that rounding might introduce.
  4. Consistent Results: Unlike rounding, which can sometimes lead to inconsistent outcomes, truncation ensures predictable and consistent results.
  5. Compatibility: The Math.trunc() method is supported in all modern browsers and environments, making it a reliable choice for cross-platform development.

Nevertheless, here are other functions you can learn to enhance your JavaScript skills.

Conclusion

In conclusion, Math.trunc is a valuable tool for JavaScript developers to truncate numerical values effectively. By removing the decimal part of a number without rounding, this function offers better performance and readability in certain scenarios. Whether you’re converting floats to integers or validating user input, Math.trunc can streamline your code and enhance its efficiency. Embrace the power of Math.trunc in your JavaScript projects and witness the difference it makes.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Glay Eliver

Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame  · View all posts by Glay Eliver →

Leave a Comment