Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Math.ceil: round upward

Math.ceil converts its argument to Number and returns the smallest integral numeric value that is not less than the input.

Version: ECMAScript 1 onward; example target: Node.js 24.22.2

Syntax

Math.ceil(x)

Math.ceil(x) applies ToNumber, then rounds toward +Infinity. Thus ceil(4.01) is 5, while ceil(-4.99) is -4. An already integral number and ±Infinity are unchanged, and NaN yields NaN. For a negative value between -1 and 0, the result is -0, so the zero sign can matter in later calculations.

Conversion means a numeric string works, null becomes 0, and a missing argument yields NaN. A Symbol or BigInt throws TypeError; an object can invoke valueOf or toString. The result remains a Number: the function cannot restore integer digits already lost to IEEE 754 precision.

The function is not a constructor and has been standard since ECMAScript 1. It is useful for page counts such as Math.ceil(total / pageSize) or required whole units. It differs from floor for negatives, from trunc in direction, and from round in its half-value rule. Validate a positive, nonzero unit size before division.

Examples

const totalItems = 101;
const pageSize = 20;
console.log(Math.ceil(totalItems / pageSize));
console.log(Math.ceil(4.01), Math.ceil(-4.99));
console.log(Object.is(Math.ceil(-0.25), -0));
console.log(Math.ceil("3.2"), Math.ceil(null));
console.log(Number.isNaN(Math.ceil()), Math.ceil(Infinity));
try { Math.ceil(1n); } catch (error) { console.log(error instanceof TypeError); }

Result

6
5 -4
true
4 0
true Infinity
true

Sources