Using Math trunc() function to coding in Javascript/JS

This JavaScript tutorial explains how to use the math characteristic known as trunc() with syntax and examples.

Description

In JavaScript, trunc() is a characteristic that is used to return the integer element of a number. It truncates the range and gets rid of all fractional digits. Because the trunc() function is a static feature of the Math object, it have to be invoked thru the placeholder object referred to as Math.

Syntax

In JavaScript, the syntax for the trunc() function is:

Math.trunc(number);

Parameters or Arguments

number

The number to truncate.

Returns

The trunc() function returns only the integer portion of a number.

Note

Math is a placeholder object that incorporates mathematical functions and constants of which trunc() is one of these functions.

Example

Let’s take a look at an instance of how to use the trunc() characteristic in JavaScript.

For example:

console.log(Math.trunc(32.65));
console.log(Math.trunc(8.1));
console.log(Math.trunc(-4.2));

In this example, we have invoked the trunc() function the use of the Math class.

We have written the output of the trunc() feature to the internet browser console log, for demonstration purposes, to show what the trunc() feature returns.

The following will be output to the internet browser console log:

32
8
-4

In this example, the first output to the console log again 32 which is 32.65 truncated to an integer value.

The 2nd output to the console log again 8 which is 8.1 truncated to an integer value.

The 1/3 output to the console log back -4 which is -4.2 truncated to an integer value.