The Math floor() method is used to round a number down to the nearest integer.
Syntax
Math.floor(x)
Parameters
x(required): It is the number whose floor value is to be calculated.
Return Value
Returns the largest integer less than or equal to a provided number
- If no argument is passed, returns negative NaN.
- If arguments cannot be converted into valid numbers, this method returns NaN.
- If a parameter is null, it returns 0.
Visual Representation
Example 1: How to Use Math floor() function
console.log(Math.floor(10));
console.log(Math.floor(-10));
console.log(Math.floor(8.7));
console.log(Math.floor(-8.7));
Output
10
-10
8
-9
Example 2: Passing 0 as an argument
console.log(Math.floor(0));
console.log(Math.floor(-0));
console.log(Math.floor(0.45));
console.log(Math.floor(0.55));
Output
0
0
0
0
Browser compatibility
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.