JavaScript Math.ceil() Method

Definition
Math.ceil(x) rounds a number up to the next whole number (an integer). It always moves toward positive infinity. 4.2 becomes 5.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax
Call Math.ceil() like this. Start with this line.
| Call | What it does |
|---|
Math.ceil(x) | Rounds x up to the next whole number. |
Built into JavaScript. Call Math.ceil() on the Math object — no import required.
Library declarations
interface Math {
ceil(x: number): number;
}
Parameters
| Parameter | Description |
|---|
| x | Required. A number. Other values are converted to a number first. |
Return Value
| Return Value | Description |
|---|
| number | The smallest integer that is greater than or equal to x. Returns NaN if x cannot be turned into a number. |
JavaScript Examples
Example 1 — Basic Use
Any number with a decimal part is rounded up.
console.log(Math.ceil(4.2)); // 5
console.log(Math.ceil(4.9)); // 5
console.log(Math.ceil(4)); // 4
console.log(Math.ceil(-4.7)); // -4
console.log(Math.ceil(0.1)); // 1
How It Works
- Round up.
4.2 and 4.9 both go up to 5. - Whole numbers.
4 is already whole, so it stays 4. - Negative numbers.
-4.7 moves up to -4 (closer to zero). - Tiny decimals. Even
0.1 rounds up to 1.
Example 2 — How Many Pages Do I Need?
If 23 items are shown 10 per page, you need 3 pages. Math.ceil() gives that.
function totalPages(items, perPage) {
return Math.ceil(items / perPage);
}
console.log(totalPages(23, 10)); // 3
console.log(totalPages(20, 10)); // 2
console.log(totalPages(21, 10)); // 3
How It Works
- Divide.
23 / 10 is 2.3. - Round up. You cannot show 2.3 pages, so
Math.ceil gives 3. - Exact fit.
20 / 10 is exactly 2, so no extra page is added.
Example 3 — Round Up to 2 Decimals
Math.ceil() only makes whole numbers. To keep 2 decimals, multiply first, then divide.
function ceilTo2(value) {
return Math.ceil(value * 100) / 100;
}
console.log(ceilTo2(4.231)); // 4.24
console.log(ceilTo2(4.2)); // 4.2
console.log(ceilTo2(9.001)); // 9.01
How It Works
- Shift.
4.231 * 100 is 423.1. - Round up.
Math.ceil(423.1) is 424. - Shift back. Divide by
100 to get 4.24.
Compatibility
Browser & Runtime Support
Math.ceil() has been part of JavaScript since ES1 and is Baseline Widely available everywhere.
✓ Baseline · ES1
Math.ceil()
Supported in every browser and Node.js version. No polyfill is needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.ceil Excellent
Bottom line: Use Math.ceil() freely. It works in all browsers and runtimes.
Frequently Asked Questions
Math.ceil(x) rounds x up to the nearest whole number. Math.ceil(4.2) is 5 and Math.ceil(4) is 4.
It moves toward zero. Math.ceil(-4.7) is -4. For negatives, "up" means closer to zero.
Divide the total items by items per page and use Math.ceil(). Math.ceil(23 / 10) is 3 pages.
Multiply by 100, use Math.ceil(), then divide by 100. Math.ceil(4.231 * 100) / 100 is 4.24.
ceil always rounds up, floor always rounds down, and round goes to the nearest whole number.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful