Skip to content

Math floor JavaScript | Rounds a number down and returns an integer value

  • by

JavaScript floor Method is used to Round a number down to its nearest integer. It required a single parameter value which is the number you want to be rounded to its nearest integer.

The Math.floor() function returns the largest integer less than or equal to a given number.

Note: If the passed argument is an integer, the value will not be rounded.

Syntax

Math.floor(value)

Math floor JavaScript Example

<!DOCTYPE html>
<html>
<body>

	<p id="demo"></p>

	<script>
  		var a = Math.floor(7.902334454);
  		document.getElementById("demo").innerHTML = a;
	
</script>

</body>
</html>

Output: 7

Q: What if we passed a non-numeric string as a parameter in the JS floor method?

Answer: If the non-numeric string passed as a parameter then it will return NaN.

var a = Math.floor("abc");
document.getElementById("demo").innerHTML = a;
Math floor JavaScript

Q: Will JS throw an error if the passing value in the JS floor method non-numeric?

Answer: No it will not throw an error if the passing value is non-numeric only. It will return NaN on the following conditions:-

  •  A non-numeric string 
  • Array with more than 1 integer 
  • Empty variable 
  • Empty string 
  • Empty array 

Do comment if you have any doubts and questions on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading