JavaScript | Math.floor() function

The Math.floor() function in JavaScript is used to round off the number passed as a parameter to its nearest integer in Downward direction of rounding i.e. towards the lesser value.

Syntax

Math.floor(value)

Parameters: This function accepts a single parameter value which is number to be rounded to its nearest integer in downward rounding method.
Returns: Result after rounding the number passed as a parameter to the function.passed as parameter.
Example:



Input  : Math.floor(4.23)
Output : 4

Input  : Math.floor(0.8)
Output : 0

Errors and Exceptions:

  • A non-numeric string passed as parameter returns NaN
  • An array with more than 1 integer passed as parameter returns NaN
  • An empty variable passed as parameter returns NaN
  • An empty string passed as parameter returns NaN
  • An empty array passed as parameter returns NaN

Below are some examples that illustrate the Math.floor() function in JavaScript:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
    document.write(Math.floor(-2) + "<br>"); 
    document.write(Math.floor(-2.56));          
</script>

chevron_right


Output:

-2
-3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- POSITIVE NUMBER EXAMPLE -->
<script type="text/javascript">
    document.write(Math.floor(2) + "<br>"); 
    document.write(Math.floor(2.56));          
</script>

chevron_right


Output:

2
2

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- STRING EXAMPLE -->
<script type="text/javascript">
    document.write(Math.floor("Geeksforgeeks"));          
</script>

chevron_right


Output:

NaN

Example 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
<script type="text/javascript">
    document.write(Math.floor(7.2+9.3));           
</script>

chevron_right


Output:

16

Supported Browsers: The browsers supported by JavaScript Math.floor( ) function are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.