JavaScript FLOOR

The JavaScript FLOOR function is a Math function that returns the largest integer value, which is less than or equal to the specified number or expression. The syntax of a JavaScript Floor Function is

Math.floor(number);
  • If the argument is a positive or negative number, the JS floor function will return the floor value.
  • If the number argument is not a number, the floor function returns NaN.
  • And if it is Null, the floor function will return Zero.

JavaScript floor Function Example

In this JS floor function example, We are going to find the floor values of different data types and display the output

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Math.floor Function </title>
</head>
<body>
  <h1> JavaScript FLOOR Function </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Dec1"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Neg_Dec1"></p>
  <p id = "Str"></p>
  <p id = "Exp"></p>
  <p id = "Null"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.floor(20);
    document.getElementById("Neg").innerHTML = Math.floor(-20);
    document.getElementById("Dec").innerHTML = Math.floor(10.05);
    document.getElementById("Dec1").innerHTML = Math.floor(10.95);
    document.getElementById("Neg_Dec").innerHTML = Math.floor(-6.05);
    document.getElementById("Neg_Dec1").innerHTML = Math.floor(-6.95);
    document.getElementById("Str").innerHTML = Math.floor("JavaScript");
    document.getElementById("Null").innerHTML = Math.floor(null);
  </script>
</body>
</html>
JavaScript FLOOR Function

ANALYSIS

  1. The first two statements, we used the FLOOR Function directly on both the Positive integer and negative integer.
  2. In the next four statements, We used the JavaScript FLOOR on both the Positive and negative Decimal values.
  3. Next, We used FLOOR Function on String value, and we know it will return NaN (Not a Number) as output
  4. The floor Math function on Null value is returning Zero as output