JavaScript Math trunc()

In this tutorial, you will learn about the JavaScript Math.trunc() with the help of examples.

The trunc() method truncates (shortens) a number and returns its integer portion.


let number = Math.trunc("420.56");

// Output: 420

trunc() Syntax

The syntax of the Math.trunc() method is:


Here, trunc() is a static method. Hence, we are accessing the method using the class name, Math.

trunc() Parameter

The trunc() method takes a single parameter:

  • number - value that needs to be truncated (shortened to integer)

trunc() Return Value

The trunc() method returns:

  • integer part of a number
  • NaN (Not a Number) for a non-numeric argument

Example 1: JavaScript Math.trunc()

// trunc() with a negative number let number1 = Math.trunc(-50.456);
// trunc() with a positive number let number2 = Math.trunc(18.645);
console.log(number2); // Output: // -50 // 18

Here, the Math.trunc() returns

  • -50 - for the negative number -50.45627
  • 18 - for the positive number 18.645

Note: The trunc() method does not round off a number, it just removes the digits after the decimal point and returns the integer portion.

Example 2: JavaScript Math.trunc() with Numeric String

// trunc() with a numeric string let number2 = Math.trunc("15.645");
console.log(number2); //Output:15

In the above example, Math.trunc("15.645") converts the numeric string to a number and truncates it.

Example 3: JavaScript Math.trunc() with Non-Numeric Argument

let string = "Luke";
// trunc() with string argument let value = Math.trunc(string);
console.log(value); // Output: NaN

In the above example, we have used the trunc() method with a string argument "Luke". That's why we get NaN as output.

Recommended readings:

Did you find this article helpful?