JavaScript is a programming language full of flexible solutions for each problem when working with integers. Once you start working with arrays containing numbers, you might need to sort them out or return a particular number.

In this case, the Math.floor() is a function that returns the largest integer less than or equal to a specified number. What makes this function so practical is its variety of use cases when working with arrays and various data sets.

Syntax and basics of Math.floor()

When it comes to syntax, this function works quite efficiently and straightforwardly. This JavaScript function is depicted as a code in the following manner:

Math.floor(x)

Please note that x is a number that represents the largest integer, which is less than or equal to the number you specify. Now, when you're aware of how to write this function syntax-wise, let's review the most basic use case of this method:

console.log(Math.floor(7.99)); // Output: 7

In this case, we indicate the number in the brackets of Math.floor() function, which is 7.99. We intend to get the number below or equal 7.99, which is 7. So, when you run this code through a compiler, the output you get is 7. While this logic seems quite okay, applying Math.floor() to larger data arrays proves useful for finding an integer in complex code applications.

The uniqueness of Math.floor()

One of the unique features of this function we review is its behavior when the number is not specified. Let's try including null to the brackets and see what result it returns to use:

console.log(Math.floor(null)); // Output: 0

We use the same code structure, not the number, but null to see what output it can bring. If you work with this example on your own, the return you'll get would be 0, but not NaN. Why's that? Not only floor() is a static method of math, but it also works with integers only, resulting in a 0 return in our example.

The good news is that you can also manipulate the Math.floor() function to work with multiple lines with numbers at the same time. Take a look:

console.log(Math.floor(2)); // Output: 2
console.log(Math.floor(3.6)); // Output: 3
console.log(Math.floor(7.5)); // Output: 7

In the first example, the returned integer cannot be less than 2, thus resulting in 2 as a return. But with cases of 3.6 or 7.5, it doesn't matter what numbers go after a solid number. Accordingly, the outputs for the second and third examples would be 3 and 7.

Another peculiar feature of Math.floor() function is how it works with negative numbers. It is no surprise that dozens of JavaScript commands work properly when the positive and negative numbers are specified. Let's try using the example that capitalizes the negative number:

console.log(Math.floor(-87.9)); // Output: -88

As you can see, we included the -87.9, which is a negative number. The logic of math.floor() function for this example is to use rounds vice versa. It means that the function rounds away from zero, returning -88 and not -87 as you might have theoretically expected.

Various number types

Because this function has varied functionality, you might have thought it is a universal command you'd be using to find an integer. However, when you're trying to work with money, Math.floor() proves to be an inferior way, compared to Math.round(), for instance. What differentiates these two approaches is how they deal with the nearest integers via rounding.

The most common example we should provide is the use of Math.round() for quite the same numbers we indicated before. Take a look:

console.log(Math.round(2)); // Output: 2
console.log(Math.round(3.6)); // Output: 4
console.log(Math.round(7.5)); // Output: 8

When using math.round(), the output will heavily rely on numbers rounded to the nearest integer. Aren't you wondering why that happened with the Math.round() function? Because it returns the value of a number rounded, the output is different from what you previously got with the Math.floor() function.

Including non-numeric types

Alright, we've already learned how the Math.floor() function works, what behavior it shows when working with negative numbers, and why the Math.round() method might work better in certain instances. What if we tell you that you can also experiment with this function by including the non-numeric types:

console.log(Math.floor("learningjavascript"));

In this case, we include the string, which is learningjavascript, and the output you get using Math.floor() function would be different than 0. In particular, this function will result in an output equaling NaN. It happens because the object you've included in the brackets is not a non-numeric type.

When you try using the Math.round() function, the return would be just the same. Thus, don't forget that you should capitalize on using only numeric types to get the desired integer output.

Test yourself

As a reminder, this function returns the largest integer less than or equal to a number you've specified. In contrast, Math.round() also returns an integer, rounded to the nearest number. Please note that if you include 0 in the brackets, the return you get equals 0, but not NaN.

Also, to acquire the lessons you've learned in this tutorial, we've prepared a few tasks you can do on your own, including:

  • Task 1. Adding NaN to brackets of Math.floor() function:

console.log(Math.floor(NaN));

Output:

NaN

As we've learned before, the Math.floor(null) will inevitably return 0, while the Math.floor(NaN) will always have an output of NaN.

  • Task 2. Compare Math.floor() and Math.round() functions using the following numbers: 5, -86.4, 187.

Output:

console.log(Math.floor(5)); // Output: 5
console.log(Math.floor(-86.4)); // Output: -87
console.log(Math.floor(187)); // Output: 187
 
console.log(Math.round(5)); // Output: 5
console.log(Math.round(-86.4)); // Output: -86
console.log(Math.round(187));// Output: 187