JavaScript Math trunc()

The JavaScript Math.trunc() function returns the integer part of a number.

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

Math.trunc(x)

trunc(), being a static method, is called using the Math class name.


Math.trunc() Parameters

The Math.trunc() function takes in:

  • x - A number

Return value from Math.trunc()

  • Returns the integer part of a number by removing the fractional digits.
  • Returns NaN if the argument can't be converted to Number type.

Example: Using Math.trunc()

// using Math.trunc()

var num = Math.trunc(1.8645);
console.log(num); // 1

var num = Math.trunc(-0.456);
console.log(num); // -0

var num = Math.trunc("420.56");
console.log(num); // 420

// Returns NaN for non-numeric types
var num = Math.trunc("JavaScript");
console.log(num); // NaN

var num = Math.trunc(NaN);
console.log(num); // NaN

Output

1
-0
420
NaN
NaN

Note: Math.trunc() does not round the number up/down, rather it just truncates the dot and digits right to it.


Recommended readings: