JavaScript Math.trunc()
Syntax & Examples


Math.trunc() static-method

The Math.trunc() static method in JavaScript returns the integer part of a number by removing any fractional digits.


Syntax of Math.trunc()

The syntax of Math.trunc() static-method is:

Math.trunc(x)

This trunc() static-method of Math returns the integer portion of the given number x, removing any fractional digits.

Parameters

ParameterOptional/RequiredDescription
xrequiredA number whose integer part is to be retrieved.

Return Type

Math.trunc() returns value of type Number.



✐ Examples

1 Using Math.trunc() with a positive number

In JavaScript, we can use the Math.trunc() method to get the integer part of a positive number.

For example,

  1. We pass the number 4.9 to the Math.trunc() method.
  2. The result, 4, is the integer part of the number.
  3. We log the result to the console using the console.log() method.

JavaScript Program

const result = Math.trunc(4.9);
console.log(result);

Output

4

2 Using Math.trunc() with a negative number

In JavaScript, we can use the Math.trunc() method to get the integer part of a negative number.

For example,

  1. We pass the number -4.9 to the Math.trunc() method.
  2. The result, -4, is the integer part of the number.
  3. We log the result to the console using the console.log() method.

JavaScript Program

const result = Math.trunc(-4.9);
console.log(result);

Output

-4

3 Using Math.trunc() with zero

In JavaScript, we can use the Math.trunc() method to get the integer part of zero.

For example,

  1. We pass the number 0.123 to the Math.trunc() method.
  2. The result, 0, is the integer part of the number.
  3. We log the result to the console using the console.log() method.

JavaScript Program

const result = Math.trunc(0.123);
console.log(result);

Output

0

Summary

In this JavaScript tutorial, we learned about trunc() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.