JavaScript Math floor() Method

The Math floor() method is used to round a number down to the nearest integer.

Syntax

Math.floor(x)

Parameters

x(required): It is the number whose floor value is to be calculated.

Return Value

Returns the largest integer less than or equal to a provided number

  1. If no argument is passed, returns negative NaN.
  2. If arguments cannot be converted into valid numbers, this method returns NaN.
  3. If a parameter is null, it returns 0.

Visual Representation

Visual Representation of JavaScript Math floor() Method

Example 1: How to Use Math floor() function

console.log(Math.floor(10));
console.log(Math.floor(-10));
console.log(Math.floor(8.7));
console.log(Math.floor(-8.7));

Output

10
-10
8
-9

Example 2: Passing 0 as an argumentVisual Representation of Passing 0 as an argument

console.log(Math.floor(0));
console.log(Math.floor(-0));
console.log(Math.floor(0.45));
console.log(Math.floor(0.55));

Output

0
0
0
0

Browser compatibility

  1. Google Chrome 1 and above
  2. Edge 12 and above
  3. Firefox 1 and above
  4. Opera 3 and above
  5. Safari 1 and above

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.