Math.ceil() is a JavaScript method that rounds a number UP to the nearest integer—regardless of its decimal value.
✅ Basic Usage
Math.ceil(4.1); // 5
Math.ceil(4.9); // 5
Math.ceil(7.0); // 7
Math.ceil(-2.3); // -2
🧠 How It Works
- Always rounds towards positive infinity.
- If the number has any decimal part → round up.
- Works with positive and negative numbers.
🔥 Real-Time Use Cases
✔ 1. Pagination
You have 53 products and want 10 per page:
const totalPages = Math.ceil(53 / 10);
console.log(totalPages); // 6
✔ 2. Splitting users into groups
12 users, 5 per group:
const groups = Math.ceil(12 / 5);
console.log(groups); // 3
✔ 3. Calculating billing / price rounding
Tax or delivery charges may require rounding up:
const amount = 99.2;
console.log(Math.ceil(amount)); // 100
✔ 4. Grid/column layouts
You want to compute how many rows are needed:
const rows = Math.ceil(items.length / columns);
✔ 5. Splitting chunks in arrays
const pages = Math.ceil(arr.length / chunkSize);
🎯 Short Interview Answer
Math.ceil()is used to round a number up to the nearest integer. It is useful in pagination, grouping, billing, chunking arrays, or any logic where you need to ensure rounding up instead of normal rounding.