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

This JavaScript tutorial explains how to use the math function referred to as floor() with syntax and examples.

Description

In JavaScript, floor() is a feature that is used to return the largest integer value that is less than or equal to a number. In different words, the floor() feature rounds a quantity down and returns an integer value. Because the floor() function is a static characteristic of the Math object, it ought to be invoked through the placeholder object referred to as Math.

Syntax

In JavaScript, the syntax for the floor() characteristic is:

Math.floor(number);

Parameters or Arguments

number The quantity used to discover the greatest integer value.

Returns

The floor() function returns the biggest integer fee that is much less than or equal to a number.

Note

Math is a placeholder object that consists of mathematical features and constants of which floor() is one of these functions.

Example

Let’s take a seem at an example of how to use the floor() characteristic in JavaScript.

For example:

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

In this example, we have invoked the floor() characteristic the use of the Math class.

We have written the output of the floor() function to the net browser console log, for demonstration purposes, to exhibit what the floor() characteristic returns.

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

32
8
-5

In this example, the first output to the console log returned 32 which is 32.65 rounded down to an integer value.

The 2d output to the console log lower back 8 which is 8.1 rounded down to an integer value.

The 0.33 output to the console log returned -5 which is -4.2 rounded down to an integer value. Notice that when dealing with negative numbers, the floor() feature rounds away from zero and in this case, lower back -5 and now not -4.