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 −
<html> <body> <script> let number1 = Math.ceil(-8.001); let number2 = Math.ceil(-5); let number3 = Math.ceil(-0.60); document.write(number1, "<br>", number2, "<br>", number3); </script> </body> </html>
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 −
<html> <body> <script> let number1 = Math.ceil(8.001); let number2 = Math.ceil(5); let number3 = Math.ceil(0.60); document.write(number1, "<br>", number2, "<br>", number3); </script> </body> </html>
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 −
<html> <body> <script> let number = Math.ceil(0); document.write(number); </script> </body> </html>
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 −
<html> <body> <script> let number1 = Math.ceil(-Infinity); let number2 = Math.ceil(Infinity); document.write(number1, "<br>", number2); </script> </body> </html>
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 −
<html> <body> <script> let number = Math.ceil(); document.write(number); </script> </body> </html>
Output
If we execute the above program, it returns "NaN" as result.