JavaScript Math.ceil() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

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.

JavaScript
Math.ceil(x)
CallWhat 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
JavaScript
Math.ceil(x)
JavaScript
interface Math {
  ceil(x: number): number;
}

Parameters

ParameterDescription
xRequired. A number. Other values are converted to a number first.

Return Value

Return ValueDescription
numberThe 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.

JavaScript
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
Try It Yourself

How It Works

  1. Round up. 4.2 and 4.9 both go up to 5.
  2. Whole numbers. 4 is already whole, so it stays 4.
  3. Negative numbers. -4.7 moves up to -4 (closer to zero).
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Divide. 23 / 10 is 2.3.
  2. Round up. You cannot show 2.3 pages, so Math.ceil gives 3.
  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.

JavaScript
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
Try It Yourself

How It Works

  1. Shift. 4.231 * 100 is 423.1.
  2. Round up. Math.ceil(423.1) is 424.
  3. Shift back. Divide by 100 to get 4.24.

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 support
Math.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.

Next: clz32()

Learn how to count leading zero bits in a 32-bit number.

clz32() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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