What is Math.floor() method and it’s Usage in JavaScript?

Having a hard time rounding down a number? Well, it can be easier using the Math.floor method in JavaScript.

In this article, we will explore the Math.floor() method in JS, a powerful tool for rounding numbers down to the nearest integer.

Read on to explore its syntax, usage, and how to combine it with Math.random() for generating random whole numbers in JavaScript.

Let’s get started and understand the Math.floor() method.

What is Math.floor() method in JavaScript?

The Math.floor() method in JavaScript is a method of the Math object that rounds a number down to the nearest integer.

It is a way to get a whole number that is smaller than or equal to a decimal number.

For instance, if you have 2.5, the whole number that is smaller than it is 2, so Math.floor(2.5) gives you 2.

If you have -2.9, the whole number that is smaller than it is -3, so Math.floor(-2.9) gives you -3.

It is like cutting off the decimal part of the number and keeping only the whole part.

In simple words, the floor() method helps you find the largest whole number that’s less than or equal to a given number.

Basically, it rounds the number down and gives you an integer.

Syntax

The syntax for the Math.floor() method is:

Math.floor(x)

Where x is a number.

Parameter

x (Required)

The Math.floor() method takes one parameter, x, which is the number you want to round down.

Return value

The Math.floor() method return a value of a number that is the largest integer smaller than or equal to the given number.

Supported browser

✔ Chrome

✔ Edge

✔ Firefox

✔ IE

✔ Opera

✔ Safari

How to use math.floor() method in JavaScript?

The Math.floor() method in JavaScript is used to round a number down to the nearest integer.

It simply means that it returns the largest integer less than or equal to the given number.

Here’s an example of how you can use the Math.floor() method:

let samplenum = 2.5;
let result = Math.floor(samplenum); ✅
console.log(result)

In this example, we pass the number 2.5 to the Math.floor() method, and it returns:

2

Which is the largest integer less than or equal to 2.5.

How to use math.floor and math.random in JavaScript?

The Math.floor() method in JavaScript is used to round a number down to the nearest integer, while the Math.random() method returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

You can use these two methods together to generate random whole numbers within a specific range.

Here’s an example of how you can do this:

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1) + min);
}

let result = getRandomInt(1, 10); // Generate a random whole number between 1 and 10

console.log(result);

As you can see, we define a function called getRandomInt() that takes two arguments: min and max.

The function uses the Math.ceil() method to round the value of min up to the nearest integer, and the Math.floor() method to round the value of max down to the nearest integer.

Then, we used the Math.random() method to generate a random floating-point number between 0 and 1, multiplies it by the range between max and min, adds min to it, and finally uses the Math.floor() method again to round the result down to the nearest integer.

This way, the function returns a random whole number between min and max, inclusive.

Output:

9

Conclusion

The Math.floor() method in JavaScript is a useful tool for rounding numbers down to the nearest integer.

It can be used on its own or in combination with other methods such as Math.random() to generate random whole numbers within a specific range.

We hope this article has provided you with enough information to help you understand what is math.floor in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.



Leave a Comment