Math.floor
Returns the largest integer less than or equal to a given number (rounds down)
Syntax
Math.floor(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | A number |
Return Value
The largest integer <= x
Examples
console.log(Math.floor(5.9)); // 5
console.log(Math.floor(-5.1)); // -6
console.log(Math.floor(5)); // 5const randomInt = Math.floor(Math.random() * 100);
console.log(randomInt); // 0-99function toWholeDollars(cents: number) {
return Math.floor(cents / 100);
}
console.log(toWholeDollars(1299)); // 12Understanding Math.floor
The Math.floor method in JavaScript returns the largest integer less than or equal to a given number (rounds down). It belongs to the Math object and is one of the most widely used methods for working with math values in modern JavaScript and TypeScript applications.
The method signature is Math.floor(x). It accepts 1 parameter: x. When called, it returns the largest integer <= x. Understanding when and how to use floor() helps you write more expressive, readable code.
Common use cases for Math.floor include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like math-ceil, math-round, math-trunc, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Math.floor is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Math Methods
Other methods in the Math object
Related Tools
More Math Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.