JavaScript Math.ceil() Method



The JavaScript Math.ceil() method accepts a numeric value as a parameter and rounds it UP to the smallest integer greater than or equal to that number.

For instance, if we pass a numeric value "7.20" to this method, it rounds it to "8" because it is the smallest integer greater than or equal to 7.20.

If we pass an empty number or non-numeric value as an argument to this method, it returns "NaN" as result. This method works similar to Math.floor() method.

Syntax

Following is the syntax of JavaScript Math.ceil() method −

Math.ceil(x);

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric value.

Return value

This method returns the smallest integer greater than or equal to the specified number.

Example 1

In the following example, we are passing negative arguments to the JavaScript Math.ceil() method −






Output

After executing, the provided numbers will be rounded UP to the nearest negative integer.

Example 2

In this example, we are passing postive arguments to the Math.ceil() method −






Output

After executing, the provided numbers will be rounded UP to the nearest integer.

Example 3

If we provide 0 to this method, it returns 0 as the result −






Output

As we can see the output, it returned 0 as result.

Example 4

Here, we are passing "Infinity" and "-Infinity" as parameters to this method −






Output

It rounds to negative (-infinity) and postive (infinity).

Example 5

If we pass an empty number or non-numeric value as an argument to this method, it returns "NaN" as output −






Output

If we execute the above program, it returns "NaN" as result.

Advertisements