Kodeclik Blog

JavaScript’s Math.floor() Method

Javascript’s Math.floor() method takes a number as input and returns the largest integer less than or equal to the input number. Let us learn how it works by writing a simple Javascript program.
We will embed our Javascript code inside a HTML page like so:
In the above HTML page you see the basic elements of the page like the head element containing the title (“JavaScript’s Math floor() Method”) and a body with an empty script tag. The Javascript code goes inside these tags.
Here is some simple code to experiment with the Math.floor() method:
The output is:
As you can see 3.14 is truncated to 3.
Here are more examples:
The output is:

JavaScript’s Math.floor() Method applied to zero

Math.floor() applied to zero yields zero:
The output is:

JavaScript’s Math.floor() Method applied to negative numbers

Let us apply Math.floor() method to a negative number:
The output is:
Note that Math.floor() applied to -57.68 does not yield -57 (because that is greater than -57.68). The floor yields -58 because that is the largest integer smaller than -57.68 (picture the number line).
If you liked the Math.floor() method, explore the Math.max() method in Javascript and the valueOf() method! Also learn how you can build upon Math.floor() to round down to different levels in Javascript!
Would you like to learn more Javascript? Checkout our blog post on how you can use Javascript Math.max() to find the max element of an array.
Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.