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.

Leave a Comment