JavaScript Math ceil()

The JavaScript Math.ceil() function rounds a number up to the next largest integer.

The syntax of the Math.ceil() function is:

Math.ceil(x)

ceil(), being a static method, is called using the Math class name.


Math.ceil() Parameters

The Math.ceil() function takes in:

  • x - A number

Return value from Math.ceil()

  • Returns the smallest integer greater than or equal to the given number.
  • Returns 0 for null.

Example: Using Math.ceil()

// using Math.ceil()

var num = Math.ceil(1.8645);
console.log(num); // 2

var num = Math.ceil(-0.456);
console.log(num); // -0

var num = Math.ceil("4");
console.log(num); // 4

// Returns 0 for null
var num = Math.ceil(null);
console.log(num); // 0

// Returns NaN for non-numeric types
var num = Math.ceil("JavaScript");
console.log(num); // NaN

var num = Math.ceil(NaN);
console.log(num); // NaN

Output

2
-0
4
0
NaN
NaN

Note: Math.ceil() returns 0 for null rather than NaN.


Recommended readings: