What is Math.floor() method and it’s Usage in JavaScript?

Having a hard time rounding down a number? Well, it can be easier using the Math.floor method in JavaScript.

In this article, we will explore the Math.floor() method in JS, a powerful tool for rounding numbers down to the nearest integer.

Read on to explore its syntax, usage, and how to combine it with Math.random() for generating random whole numbers in JavaScript.

Let’s get started and understand the Math.floor() method.

What is Math.floor() method in JavaScript?

The Math.floor() method in JavaScript is a method of the Math object that rounds a number down to the nearest integer.

It is a way to get a whole number that is smaller than or equal to a decimal number.

For instance, if you have 2.5, the whole number that is smaller than it is 2, so Math.floor(2.5) gives you 2.

If you have -2.9, the whole number that is smaller than it is -3, so Math.floor(-2.9) gives you -3.

It is like cutting off the decimal part of the number and keeping only the whole part.

In simple words, the floor() method helps you find the largest whole number that’s less than or equal to a given number.

Basically, it rounds the number down and gives you an integer.

Syntax

The syntax for the Math.floor() method is:

Math.floor(x)

Where x is a number.

Parameter

x (Required)

The Math.floor() method takes one parameter, x, which is the number you want to round down.

Return value

The Math.floor() method return a value of a number that is the largest integer smaller than or equal to the given number.

Supported browser

✔ Chrome

✔ Edge

✔ Firefox

✔ IE

✔ Opera

✔ Safari

How to use math.floor() method in JavaScript?

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

It simply means that it returns the largest integer less than or equal to the given number.

Here’s an example of how you can use the Math.floor() method:

let samplenum = 2.5;
let result = Math.floor(samplenum); ✅
console.log(result)

In this example, we pass the number 2.5 to the Math.floor() method, and it returns:

2

Which is the largest integer less than or equal to 2.5.

How to use math.floor and math.random in JavaScript?

The Math.floor() method in JavaScript is used to round a number down to the nearest integer, while the Math.random() method returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

You can use these two methods together to generate random whole numbers within a specific range.

Here’s an example of how you can do this:

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1) + min);
}

let result = getRandomInt(1, 10); // Generate a random whole number between 1 and 10

console.log(result);

As you can see, we define a function called getRandomInt() that takes two arguments: min and max.

The function uses the Math.ceil() method to round the value of min up to the nearest integer, and the Math.floor() method to round the value of max down to the nearest integer.

Then, we used the Math.random() method to generate a random floating-point number between 0 and 1, multiplies it by the range between max and min, adds min to it, and finally uses the Math.floor() method again to round the result down to the nearest integer.

This way, the function returns a random whole number between min and max, inclusive.

Output:

9

Conclusion

The Math.floor() method in JavaScript is a useful tool for rounding numbers down to the nearest integer.

It can be used on its own or in combination with other methods such as Math.random() to generate random whole numbers within a specific range.

We hope this article has provided you with enough information to help you understand what is math.floor in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.



Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment