JavaScript math floor function is one of the many inbuilt methods that is defined in Math Class. If you are looking for javascript math.floor and how to use it then this article will help you.

When dealing with numbers, you might need to round them off. It is then the floor() method is used.

What does Math.floor do?
The JavaScript floor function rounds a number downwards to the nearest integer value and returns it.

Usage of :
We need to use the dot(.) operator with the Math Class to use the floor method.

Syntax:

Math.floor(x);

In the above syntax, ‘x’ is any given number passed as a parameter to be round off.
Note– If the number passed is an integer then, it has no effect.

JS Math.floor Examples

What is Math.floor(3.6)?
Let’s check for a positive decimal number 3.6.


let x = Math.floor(3.6);
console.log(x); // 3

Thus, if we round of 3.6 using Math.floor in JavaScript, the answer is 3.

What about Math.floor(5.5) evaluates to?


let x = Math.floor(5.5);
console.log(x); //  5

Again, for 5.5 if we use Math.floor to round it, we get 5 as the returned value.

What about negative numbers?


let a = Math.floor(-4.1);
console.log(a);             // 5

let b = Math.floor(-4.9);
console.log(b);             // 5

let c = Math.floor(-4.5);
console.log(c);             // 5

For negative decimal numbers also, math.floor javascript rounds the number to the downward nearest integer.

Finally, what if we pass an Integer?


let a = Math.floor(0);
console.log(a);             // 0

let b = Math.floor(7);
console.log(b);             // 7

As mentioned earlier, If we pass an integer as a parameter to the floor method, the value returns are the same.

Let us know in the comments if you want more such examples and want to see the usage of other in-built methods in JS. Also, you might be interested to read How to Square a Number in JavaScript?

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed.