Math.ceil() in JavaScript with Examples

Learn how Math.ceil() works in JavaScript with rounding up examples, negative numbers, and practical integer conversion cases.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Math.ceil() in JavaScript with Examples

Math.ceil() rounds a number up to the next integer in JavaScript. It is useful when you want to make sure you always have enough units, pages, items, or slots after a decimal calculation.

The method belongs to the Math object and is often compared with Math.round() and Math.floor(). If you need the downward version too, Math.floor in JavaScript is the related article.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Method 1: Round a decimal number up

Math.ceil() always moves the number to the next integer at or above the original value.

javascript
console.log("ceil-1:", Math.ceil(4.01));
console.log("ceil-2:", Math.ceil(3.98));
console.log("ceil-3:", Math.ceil(123.45));
Output

You should see 3 lines, in order: ceil-1: 5, ceil-2: 4, ceil-3: 124.

This is the right choice when you need to round up regardless of the decimal part.


Method 2: Compare ceil with round

Math.round() rounds to the nearest integer, while Math.ceil() always goes up.

javascript
const x = 6.78;
const y = 5.34;

console.log("round-x:", Math.round(x));
console.log("round-y:", Math.round(y));
console.log("ceil-x:", Math.ceil(x));
console.log("ceil-y:", Math.ceil(y));
Output

You should see 4 lines, in order: round-x: 7, round-y: 5, ceil-x: 7, ceil-y: 6.

Use this comparison when you are choosing between a near-round and always-round-up rule.


Method 3: Use Math.ceil() with negative numbers

For negative numbers, rounding up moves toward zero.

javascript
const negativeX = -6.78;
const negativeY = -5.34;

console.log("ceil-negative-x:", Math.ceil(negativeX));
console.log("ceil-negative-y:", Math.ceil(negativeY));
Output

You should see 2 lines, in order: ceil-negative-x: -6, ceil-negative-y: -5.

This is important when you are computing counts, page totals, or any number that can go below zero.


Summary

Math.ceil() is the JavaScript rounding-up method. Use it when you want the next highest integer, and remember that negative numbers move toward zero rather than away from it.


Official documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …