JavaScript Math ceil()

The ceil() method rounds a decimal number up to the next largest integer and returns it. That is, 4.3 will be rounded to 5 (next largest integer).

Example


ceil() Syntax

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

Math.ceil(number)

Here, ceil() is a static method. Hence, we are accessing the method using the class name, Math.


ceil() Parameter

The ceil() method takes in a single parameter:

  • number - value that is rounded off to its nearest largest integer

ceil() Return Value

The ceil() method returns:

  • the nearest largest integer after rounding off the specified number
  • NaN (Not a Number) for a non-numeric argument

Example 1: JavaScript Math.ceil() with Positive Numbers

Here, we have used the ceil() method to round decimal values, 23.8 to 24 and 87.1 to 88.


Example 2: Math.ceil() with Negative Numbers

Here, Math.ceil() rounds the number -3.8 to -3 because mathematically, -3 is larger than -3.8. Similarly, the method rounds -7.1 to -7.


Example 3: Math.ceil() with Numeric String

Here, we have used the numeric string "2.490" with the ceil() method. In this case, the numeric string is converted to a number.

Then, the method rounds the number to its next largest integer, 3.


Example 4: Math.ceil() with Non-Numeric Argument

In the above example, we have tried to calculate the sign of the string "JavaScript". Hence, we get NaN as the output.


Recommended readings: