Math.ceil()
Math.ceil(x) Returns:
number · Added in vES1 · Updated March 13, 2026 · Math math ceil ceiling rounding
The Math.ceil() method returns the smallest integer greater than or equal to a given number. Think of it as “rounding up” — always toward positive infinity, regardless of the fractional part.
Syntax
Math.ceil(x)
Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | The number to ceil |
Return Value
- The smallest integer greater than or equal to x
What is Ceiling?
The “ceiling” of a number is the smallest integer that is not less than it. In mathematical notation, ceil(x) is the unique integer n such that n-1 < x ≤ n. Visually, you’re always rounding up to the next whole number.
Examples
Basic rounding with positive and negative numbers
// Positive numbers — rounds up to next integer
Math.ceil(4.2); // 5
Math.ceil(4.8); // 5
Math.ceil(4.0); // 4 (already an integer)
// Negative numbers — rounds UP toward zero
Math.ceil(-4.2); // -4
Math.ceil(-4.8); // -4
Math.ceil(-4.0); // -4 (already an integer)
// Notice: ceil(-4.8) gives -4, NOT -5
// This is different from Math.floor()
The key insight with negative numbers: ceil() always rounds toward positive infinity. So -4.8 becomes -4 because -4 > -4.8.
Practical use: Calculating pages needed
A classic use case is determining how many pages are needed to display a collection of items:
const pageSize = 10;
const totalItems = 95;
function getPageCount(totalItems, pageSize) {
return Math.ceil(totalItems / pageSize);
}
getPageCount(95, 10); // 10 pages
getPageCount(100, 10); // 10 pages
getPageCount(101, 10); // 11 pages
// Real-world example: paginating a blog
const posts = [
{ title: "Math.ceil()" }, { title: "Math.ceil()" }, { title: "Math.ceil()" },
{ title: "Math.ceil()" }, { title: "Math.ceil()" }, { title: "Math.ceil()" },
{ title: "Math.ceil()" }, { title: "Math.ceil()" }, { title: "Math.ceil()" },
{ title: "Math.ceil()" }, { title: "Math.ceil()" }
];
const ITEMS_PER_PAGE = 4;
const totalPages = Math.ceil(posts.length / ITEMS_PER_PAGE);
console.log(`Need ${totalPages} pages for ${posts.length} posts`);
// "Need 3 pages for 11 posts"
Comparison: ceil vs floor vs round
This table clarifies the differences:
// Comparison for positive numbers
Math.ceil(3.2); // 4 (rounds up)
Math.floor(3.2); // 3 (rounds down)
Math.round(3.2); // 3 (nearest)
// Comparison for negative numbers
Math.ceil(-3.2); // -3 (rounds up, toward zero)
Math.floor(-3.2); // -4 (rounds down, away from zero)
Math.round(-3.2); // -3 (nearest)
// The .5 cases
Math.ceil(3.5); // 4
Math.floor(3.5); // 3
Math.round(3.5); // 4
Math.ceil(-3.5); // -3
Math.floor(-3.5); // -4
Math.round(-3.5); // -3
Edge Cases
Math.ceil(Infinity); // Infinity
Math.ceil(-Infinity); // -Infinity
Math.ceil(NaN); // NaN
Math.ceil(0); // 0
Math.ceil(-0); // -0
Math.ceil(42); // 42 (integers return unchanged)
Key Behaviors
- Always rounds toward positive infinity
- For negative numbers, this means rounding toward zero
- Differs from
Math.floor()which rounds toward negative infinity - Differs from
Math.round()which rounds to the nearest integer - A static method — call it as
Math.ceil(), not on an instance
See Also
- math::floor — Round down
- math::round — Round to nearest
- math::trunc — Remove fractional digits