Math.ceil(x) rounds a number toward positive infinity and returns the smallest integer greater than or equal to the input. For negative decimals this often means a value closer to zero.
console.log(Math.ceil(7.004)); // 8
console.log(Math.ceil(-7.004)); // -7Math.ceil(number)console.log(Math.ceil(11.257)); // 12
console.log(Math.ceil(1.56)); // 2
console.log(Math.ceil(4)); // 4console.log(Math.ceil(-3.564)); // -3
console.log(Math.ceil(-7.164)); // -7console.log(Math.ceil()); // NaN
console.log(Math.ceil("xyz")); // NaN
console.log(Math.ceil("12.2")); // 13See NaN for invalid numeric results.
const value = 5.42;
console.log(Math.ceil(value)); // 6
console.log(Math.floor(value)); // 5
console.log(Math.round(value)); // 5const value = -5.67;
console.log(Math.ceil(value)); // -5
console.log(Math.floor(value)); // -6
console.log(Math.round(value)); // -6Compare with Math.floor(), Math.round(), and toFixed().
Typical uses include calculating how many pages are required, how many containers are needed for a quantity, or how many billing units are necessary when partial units must count as a whole unit.
const items = 53;
const perPage = 10;
const pages = Math.ceil(items / perPage);
console.log(pages); // 6const raw = "-12.78";
const value = Number(raw);
if (Number.isFinite(value)) {
console.log(Math.ceil(value)); // -12
}Math.ceil(Infinity) remains Infinity. Very small negative values can produce -0; this is normally displayed as 0, although Object.is(result, -0) can distinguish it. Numeric strings are coerced, but explicit conversion is easier to validate and maintain.
console.log(Math.ceil(Infinity)); // Infinity
console.log(Object.is(Math.ceil(-0.1), -0)); // trueDo not describe ceil() simply as “rounding away from zero.” That is incorrect for negative numbers. Think of it as moving toward positive infinity. Also remember that rounding a number is different from formatting it for display; use toFixed() when a fixed number of decimal places is the actual requirement.
You can also read the PHP ceil() tutorial.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.