Syntax
The syntax of the Math.floor() method is quite simple:
Math.floor(x);
Key Points
- Math Object: Math is a built-in JavaScript object that contains methods and constants for performing mathematical functions.
- Static Method: Math.floor() is a static method, meaning it is called directly using the Math object and not an instance of it.
Parameters
The Math.floor() method takes a single parameter:
- x (Required): The number that you want to round down to the nearest integer.
Return Value
The Math.floor() function returns the largest integer less than or equal to the given number. Let’s take a few examples to show the return values:
console.log(Math.floor(4.7));
console.log(Math.floor(-4.2));
console.log(Math.floor(0.9));
console.log(Math.floor(-0.5));
console.log(Math.floor(5));

You can also try this code with Online Javascript Compiler
Run Code
Output:
4
-5
0
-1
5
As you can see from the examples above:
- When the input is a positive number, Math.floor() returns the nearest integer less than or equal to it.
- When the input is a negative number, Math.floor() returns the nearest integer less than or equal to it (which means it rounds towards negative infinity).
- When the input is already an integer, Math.floor() returns that integer unchanged.
Always Remember-: Math.floor() always rounds down, even for negative numbers. If you want to round a number to the nearest integer (either up or down), you can use the Math.round() function instead.
Example
Let's look at some practical examples to understand how Math.floor() works.
Example 1: Rounding Down Positive Numbers
let number = 5.9;
let result = Math.floor(number);
console.log("Rounded down:", result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Rounded down: 5
Explanation:
The Math.floor() method rounds 5.9 down to 5, which is the largest integer less than or equal to 5.9.
Example 2: Rounding Down Negative Numbers
let number = -3.7;
let result = Math.floor(number);
console.log("Rounded down:", result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Rounded down: -4
Explanation:
For negative numbers, Math.floor() still rounds down, moving further away from zero. Hence, -3.7 becomes -4.
Example 3: Using with Variables
let scores = [5.2, 6.8, 7.4];
let roundedScores = scores.map(score => Math.floor(score));
console.log("Rounded scores:", roundedScores);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Rounded scores: [5, 6, 7]
Explanation:
In this example, we used the map() function to apply Math.floor() to each element in the scores array. The result is a new array with all values rounded down.
Example 4: Rounding Down Zero and Integers
console.log(Math.floor(0));
console.log(Math.floor(10));
console.log(Math.floor(-10));

You can also try this code with Online Javascript Compiler
Run Code
Output:
0
10
-10
Explanation:
For integers, Math.floor() returns the same value. Zero is treated as a special case, where the result is always 0.
Supported Browsers
The Math.floor() method is supported in all major browsers, including:
- Google Chrome
- Mozilla Firefox
- Microsoft Edge
- Safari
- Opera
Mobile Browser Support:
- Android WebView
- Chrome for Android
- Firefox for Android
- Safari on iOS
In short, you can use Math.floor() without worrying about browser compatibility.
Frequently Asked Questions
What is the purpose of the Math.floor() method in JavaScript?
The Math.floor() method is used to round down a given number to the nearest integer that is less than or equal to the number.
How does Math.floor() handle negative numbers?
For negative numbers, Math.floor() rounds down further away from zero. For example, Math.floor(-3.2) returns -4.
Is Math.floor() compatible with all browsers?
Yes, Math.floor() is supported in all modern browsers, making it reliable for web development.
Conclusion
In this article, we covered the Math.floor() method in JavaScript. We discussed its syntax, how it works, and its behavior with different types of numbers. With practical examples and explanations, you should now be able to use Math.floor() effectively in your JavaScript projects. Whether you're manipulating arrays or working with complex mathematical operations, this method is a handy tool to have in your coding arsenal.
You can also check out our other blogs on Code360.