JavaScript Math.floor() | Round Down to nearest integer

The JavaScript Math.floor() method is used when we need to round a number to its nearest smallest integer. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

   <script>
      let num = 0.01;
      document.getElementById("xyz").innerHTML = Math.floor(num);
   </script>
   
</body>
</html>
Output

JavaScript Math.floor() Syntax

The syntax of Math.floor() method in JavaScript is:

Math.floor(x)

The parameter x is required, refers to a number, which is going to round Down to its nearest integer value.

JavaScript Math.floor() Example

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p>Math.floor(-5.01) = <span id="span1"></span></p>
   <p>Math.floor(-5.4) = <span id="span2"></span></p>
   <p>Math.floor(-5.5) = <span id="span3"></span></p>
   <p>Math.floor(-5.9) = <span id="span4"></span></p>
   <p>Math.floor(0) = <span id="span5"></span></p>
   <p>Math.floor(5.01) = <span id="span6"></span></p>
   <p>Math.floor(5.4) = <span id="span7"></span></p>
   <p>Math.floor(5.5) = <span id="span8"></span></p>
   <p>Math.floor(5.9) = <span id="span9"></span></p>

   <script>
      document.getElementById("span1").innerHTML = Math.floor(-5.01);
      document.getElementById("span2").innerHTML = Math.floor(-5.4);
      document.getElementById("span3").innerHTML = Math.floor(-5.5);
      document.getElementById("span4").innerHTML = Math.floor(-5.9);
      document.getElementById("span5").innerHTML = Math.floor(0);
      document.getElementById("span6").innerHTML = Math.floor(5.01);
      document.getElementById("span7").innerHTML = Math.floor(5.4);
      document.getElementById("span8").innerHTML = Math.floor(5.5);
      document.getElementById("span9").innerHTML = Math.floor(5.9);
  </script>
   
</body>
</html>
Output

Math.floor(-5.01) =

Math.floor(-5.4) =

Math.floor(-5.5) =

Math.floor(-5.9) =

Math.floor(0) =

Math.floor(5.01) =

Math.floor(5.4) =

Math.floor(5.5) =

Math.floor(5.9) =

Please note: -6 is smaller than -5.01, -5.4, 5.5, and -5.9.

Other methods to do similar job

Here are the list of methods that are used to either round a number in other way, or to do similar type of job.

JavaScript Online Test


« Previous Tutorial Next Tutorial »