JavaScript Math.ceil()
Syntax & Examples
Math.ceil() static-method
The Math.ceil() method in JavaScript returns the smallest integer greater than or equal to a given number. It is a static method of Math, meaning it is always used as Math.ceil() and not as a method of a Math object created (Math is not a constructor).
Syntax of Math.ceil()
The syntax of Math.ceil() static-method is:
Math.ceil(x)
This ceil() static-method of Math returns the smallest integer greater than or equal to x.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
x | required | A number for which to return the smallest integer greater than or equal to it. |
Return Type
Math.ceil() returns value of type Number
.
✐ Examples
1 Using Math.ceil() with a positive number
In this example, we use the Math.ceil()
method to find the smallest integer greater than or equal to 4.2.
For example,
- We call
Math.ceil()
with 4.2 as the argument. - The method returns the smallest integer greater than or equal to 4.2.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.ceil(4.2));
Output
5
2 Using Math.ceil() with a negative number
In this example, we use the Math.ceil()
method to find the smallest integer greater than or equal to -4.2.
For example,
- We call
Math.ceil()
with -4.2 as the argument. - The method returns the smallest integer greater than or equal to -4.2.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.ceil(-4.2));
Output
-4
3 Using Math.ceil() with an integer
In this example, we use the Math.ceil()
method on an integer, which returns the integer itself.
For example,
- We call
Math.ceil()
with 7 as the argument. - The method returns the smallest integer greater than or equal to 7.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.ceil(7));
Output
7
Summary
In this JavaScript tutorial, we learned about ceil() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.