Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

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:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JavaScript’s Math floor() Method</title>
</head>

<body>
  <script>
  </script>
</body>

</html>

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:

<script>
  pi = 3.14
  document.write(pi)
  document.write('<BR>')
  document.write(Math.floor(pi))
</script>

Javascript Math.floor() method

The output is:

3.14
3

As you can see 3.14 is truncated to 3.

Here are more examples:

<script>
  document.write("3.14 -> Math.floor() -> " + Math.floor(3.14))
  document.write('<BR>')
  document.write("3.99 -> Math.floor() -> " + Math.floor(3.99))
  document.write('<BR>')
  document.write("0.56 -> Math.floor() -> " + Math.floor(0.56))
</script>

The output is:

3.14 -> Math.floor() -> 3
3.99 -> Math.floor() -> 3
0.56 -> Math.floor() -> 0

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

Math.floor() applied to zero yields zero:

<script>
  document.write("0.00 -> Math.floor() -> " + Math.floor(0.00))
  document.write('<BR>')
</script>

The output is:

0.00 -> Math.floor() -> 0

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

Let us apply Math.floor() method to a negative number:

<script>
  document.write("-57.68 -> Math.floor() -> " + Math.floor(-57.68))
  document.write('<BR>')
</script>

The output is:

-57.68 -> Math.floor() -> -58

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.

Kodeclik sidebar newsletter

Join our mailing list

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

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.