JavaScript Math.ceil()

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)); // -7

Math.ceil() syntax Top ↑

Math.ceil(number)

Positive numbers Top ↑

console.log(Math.ceil(11.257)); // 12
console.log(Math.ceil(1.56));   // 2
console.log(Math.ceil(4));      // 4

Negative numbers Top ↑

console.log(Math.ceil(-3.564)); // -3
console.log(Math.ceil(-7.164)); // -7

Invalid and missing values Top ↑

console.log(Math.ceil());          // NaN
console.log(Math.ceil("xyz"));     // NaN
console.log(Math.ceil("12.2"));    // 13

See NaN for invalid numeric results.

Math.ceil() vs Math.floor() vs Math.round() Top ↑

const value = 5.42;
console.log(Math.ceil(value));  // 6
console.log(Math.floor(value)); // 5
console.log(Math.round(value)); // 5
const value = -5.67;
console.log(Math.ceil(value));  // -5
console.log(Math.floor(value)); // -6
console.log(Math.round(value)); // -6

Compare with Math.floor(), Math.round(), and toFixed().

Common uses of Math.ceil() Top ↑

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); // 6

Handling user input safely Top ↑

const raw = "-12.78";
const value = Number(raw);
if (Number.isFinite(value)) {
  console.log(Math.ceil(value)); // -12
}

Infinity, -0 and coercion Top ↑

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)); // true

Common Math.ceil() mistakes Top ↑

Do 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.




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer