In JavaScript, Math.floor() is a built-in function that rounds a number down to the nearest integer.

Syntax:

Math.floor(x)
  • x is the number you want to round down.

Example:

Math.floor(4.7);  // returns 4
Math.floor(4.1);  // returns 4
Math.floor(-4.7); // returns -5

When is it used?

Math.floor() is often used with Math.random() to generate random integers:

// Random integer between 0 and 9
let randomInt = Math.floor(Math.random() * 10);