JavaScript Math floor: How It Works with Examples

javascript math floor

JavaScript gives you several ways to round numbers, but not all of them round the same way. If you want to drop the decimal and keep the lower whole number, you use Math.floor().

It works well when you handle prices, time, or indexes where rounding up can break logic. In this article, you will see how Math.floor() works in JavaScript and where to use it.

Understand the Math.floor() in JavaScript

Math.floor() is a method that rounds a number down to the nearest whole number. It does not care how close the decimal is to the next number. It always returns the lower number.

Here is the syntax:

Math.floor(number)

It takes one argument. That can be any number (positive, negative, float, or integer). The result is the largest integer less than or equal to the input.

This function always returns a smaller or equal whole number. It does not look at the decimal part. Even if the decimal is 0.999, Math.floor() still moves the number down.

Here is a quick example:

Math.floor(4.9) // returns 4
Math.floor(4.1) // also returns 4
Math.floor(-4.1) // returns -5

Here are the use cases:

  • Create random whole numbers from floats
  • Remove decimals from user input or calculations
  • Round down to set price points or discounts
  • Get whole-number indexes for pagination
  • Work with time values like hours, minutes, or dates
  • Avoid rounding up by mistake in loops or counters

Examples of JavaScript Math.floor()

Let us look at some real use cases and how Math.floor() behaves in each.

Round positive decimal numbers:

Math.floor(7.8) // returns 7
Math.floor(2.3) // returns 2

The decimal part gets dropped. The number moves down.

Round negative decimal bumbers:

Math.floor(-3.2) // returns -4
Math.floor(-5.9) // returns -6

It still rounds down. So for negative numbers, the result becomes smaller.

Create random integers with Math.random():

let randomInt = Math.floor(Math.random() * 10); // 0 to 9

This is a common way to pick a random item from a list or shuffle data.

Handle price calculations or discounts:

let fullPrice = 19.99;
let dealPrice = Math.floor(fullPrice); // returns 19

Work with time, pagination, or indexing:

let minutes = 126;
let hours = Math.floor(minutes / 60); // returns 2

You also use it to pick list items safely:

let page = Math.floor(currentIndex / itemsPerPage);

Browser Support for Math.floor() in JavaScript

You can use Math.floor() in all major browsers. It works in:

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Safari
  • Opera
  • Internet Explorer (version 6 and above)

It has been supported for a long time, even in older versions. You do not need a fallback or polyfill. That means you can use Math.floor() without any worries about browser issues.

Wrapping Up

In this article, you learned how JavaScript Math.floor() works and where you can use it. You saw how it always rounds numbers down and how it handles both positive and negative values. You also looked at real examples like price rounding, time logic, and random numbers.

Here is a quick recap:

  • Math.floor() returns the largest whole number less than or equal to the input
  • It drops the decimal without rounding up
  • It works with positive and negative numbers
  • You can use it for price logic, pagination, or time math
  • It has full browser support and needs no fallback

Thank you for reading. Click here to see more articles about JavaScript built-in functions.

FAQs

How to use Math.floor in JavaScript?

You pass a number to Math.floor() and it returns the largest whole number less than or equal to that number.
Math.floor(4.7); // returns 4
Math.floor(-4.7); // returns -5
You can use it with Math.random() to get random integers:
let random = Math.floor(Math.random() * 10); // returns 0 to 9

What does the Math.floor() method do?

Math.floor() rounds a number down to the nearest whole number. It does not round up. It always moves toward the smaller integer.
Math.floor(5.9); // returns 5
Math.floor(-5.1); // returns -6

Does Math.floor() Work on Strings?

Yes, it works if the string holds a valid number. JavaScript will try to convert it.
Math.floor("7.8"); // returns 7
Math.floor("abc"); // returns NaN
If the string cannot convert to a number, the result is NaN.

What If Input Is Already an Integer?

Math.floor() returns the same number. It does not change anything.
Math.floor(6); // returns 6
Math.floor(-2); // returns -2

Can You Use It with BigInt?

No, you cannot. Math.floor() does not support BigInt. If you try, JavaScript throws a TypeError.
Math.floor(10n); // TypeError: Cannot convert a BigInt value to a number
You must use regular numbers with Math.floor(). If you work with BigInt, use other logic to round manually.

Similar Reads

JavaScript unshift Function: How it Works with Examples

The JavaScript unshift function adds one or more elements at the start of an array. It shifts existing items to…

JavaScript Introduction: What is JavaScript?

The current essay is devoted to the basic principles and introduction of JavaScript. This language no longer needs to be…

JavaScript Math tan: The Tangent of an Angle in Radians

JavaScript Math.tan() finds the tangent of an angle in radians. You use it when you need to solve angle-based math.…

How to Use the Ternary Operator in JavaScript

The ternary operator in JavaScript keeps your code short. It helps you write conditions inside one line. JavaScript lets you…

How Does JavaScript Work to Run Code in the Web Browser

Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…

JavaScript Optional Chaining “?”: How it Works with Examples

JavaScript optional chaining lets you access object values without runtime errors. It checks if a property exists before access and…

JavaScript toSorted Function Guide with Examples

This guide shows how toSorted function works in JavaScript with arrays. It covers syntax, rules, and examples for numbers, text,…

JavaScript Coding Style Guide for Beginners

JavaScript code follows some rules that help both new and old developers. A project without rules in code creates trouble…

JavaScript: How to Add JS to HTML

The development of full-featured and interesting pages cannot be done without JavaScript which makes it possible to animate plain HTML…

JavaScript Math trunc: How to Removes Decimals

JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the…

Previous Article

JavaScript Math trunc: How to Removes Decimals

Next Article

JavaScript Math round(): How It Works With Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.