JavaScript Math ceil()

In this tutorial, you will learn about the JavaScript Math.ceil() method with the help of examples.

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

``````let number = Math.ceil(4.3);
console.log(number);

// Output: 5``````

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

``````let result1 = Math.ceil(23.8);
console.log(result1);
// Output: 24

let result2 = Math.ceil(87.1);
console.log(result2);
// Output: 88``````

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

``````let result1 = Math.ceil(-3.8);
console.log(result1);
// Output: -3

let result2 = Math.ceil(-7.1);
console.log(result2);
// Output:  -7``````

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

``````// ceil() with a numeric string
let number3 = Math.ceil("2.490");
console.log(number3);
// Output: 3``````

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

``````// ceil() with string as argument
let value = Math.ceil("JavaScript");
console.log(value);
// Output: NaN``````

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