Math.floor() is a Math class object method in JavaScript used to round out the given number into the immediate precedent integer.
It takes one parameter as an argument, which is the number that we want to floor down. If the provided value is positive or negative, the Math.floor() method will return the largest integer that is less than or equal to the number original number provided to the method. But on passing 0 or null inside the method zero (0) will be returned.
This method is a built-in and static method of the Math class object in JavaScript.
Table of Contents
Syntax
Math.floor(number);
Here,
Parameter: It is a number that we will pass into the method parameter to floor down.
Return Value: This method returns the largest integer which is less than or equal to the entered number.
In a scenario when the parameter passed, is an empty string, empty array, or null, the math.floor() method will return zero (0)
It may show an error or exception in the conditions when the method’s returned value is either NaN or 0.
Let’s look at an example with different input parameters to see the different results discussed above.
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<p id="demo5"></p>
<p id="demo6"></p>
<p id="demo7"></p>
<p id="demo8"></p>
<p id="demo9"></p>
<p id="demo10"></p>
<p id="demo11"></p>
<script>
fm1 = Math.floor(-3.05);
fm2 = Math.floor(4.13);
fm3 = Math.floor(0);
fm4 = Math.floor(null);
fm5 = Math.floor('');
fm6 = Math.floor('A');
fm7 = Math.floor([]);
fm8 = Math.floor([3]);
fm9 = Math.floor([1,2]);
fm10 = Math.floor();
fm11 = Math.floor(undefined);
document.getElementById("demo1").innerHTML = "Math.floor(-3.05) = " + fm1;
document.getElementById("demo2").innerHTML = "Math.floor(4.13) = " + fm2;
document.getElementById("demo3").innerHTML = "Math.floor(0) = " + fm3;
document.getElementById("demo4").innerHTML = "Math.floor(null) = " + fm4;
document.getElementById("demo5").innerHTML = "Math.floor('') = " + fm5;
document.getElementById("demo6").innerHTML = "Math.floor('A') = " + fm6;
document.getElementById("demo7").innerHTML = "Math.floor([]) = " + fm7;
document.getElementById("demo8").innerHTML = "Math.floor([3.9]) = " + fm8;
document.getElementById("demo9").innerHTML = "Math.floor([1.3,2.2]) = " + fm9;
document.getElementById("demo10").innerHTML = "Math.floor() = " + fm10;
document.getElementById("demo11").innerHTML = "Math.floor(undefined) = " + fm11;
</script>
</body>
</html>
Output

Conclusion
This article discussed the JavaScript Floor method, i.e., Math.floor(). It is a built-in and static method of the Math object in JavaScript. This method is used to convert a given number into it’s immediate preceding integer. However, it will return inappropriate results if the user passes an invalid number in the parameter. We saw an example in which we passed different valid and invalid numbers returning different types of results. JavaScript Math class contains a lot more methods dealing with various mathematical operations on the numbers.