JavaScript Math.ceil() Method
-
Syntax of JavaScript
Math.ceil()
Method -
Example 1: Use the
Math.ceil()
Method to Get Nearest Integer of a Number in Decimal Form -
Example 2: Use
Math.ceil()
andMath.round()
to Get and Compare the Rounded Numbers -
Example 3: Use the
Math.ceil()
Method on a String Value -
Example 4: Use the
Math.ceil()
Method on Addition of Two Values -
Example 5: Use the
Math.ceil()
Method on an Empty Value
The Math.ceil()
method takes any number and round it to the nearest integer without decreasing the value. In JavaScript, ceil
is used for Math
objects, and the syntax can’t be changed.
Syntax of JavaScript Math.ceil()
Method
Math.ceil(x);
Parameters
x |
A number is required. |
Return
This method returns the nearest integer of the number after rounding it up.
Example 1: Use the Math.ceil()
Method to Get Nearest Integer of a Number in Decimal Form
We use the method Math.ceil()
to get a rounded-up integer of a number in decimal form to increase the value. In the example below, we used the Math.ceil()
method to get the nearest integer of a number.
let num = 0.5;
console.log(Math.ceil(num));
Output:
1
Example 2: Use Math.ceil()
and Math.round()
to Get and Compare the Rounded Numbers
We convert the decimal numbers to the nearest integer using Math.ceil()
and Math.round()
methods. However, both methods give different outputs if the number in decimal form is less than .5
.
In this example, we have used the ceil()
and round()
methods to round 1.2
to the nearest integer.
let num = 1.2;
console.log(Math.ceil(num));
console.log(Math.round(num));
Output:
2
1
Example 3: Use the Math.ceil()
Method on a String Value
When we use the string value as a parameter of the Math.ceil()
method, it always returns the NaN
(Not a Number
) that users also can see in the output of the example below.
let str = "Delft";
let ceilValue = Math.ceil(str);
console.log(ceilValue);
console.log(Math.ceil("stack"));
Output:
NaN
NaN
Example 4: Use the Math.ceil()
Method on Addition of Two Values
Users can use the Math.ceil()
method to add two or more numbers after rounding up the numbers.
In the example below, we have taken two number values and added them differently. In the sum1
, we have stored the ceil
value of the sum of both numbers, and in the sum2
, we have stored the sum of two different numbers after rounding them up separately.
let num1 = 10.2;
let num2 = 10.4;
let sum1 = Math.ceil(num1+num2);
let sum2 = Math.ceil(num1) + Math.ceil(num2);
console.log(sum1);
console.log(sum2);
Output:
21
22
Example 5: Use the Math.ceil()
Method on an Empty Value
We will understand what happens if we pass the empty value as a parameter of the Math.ceil()
method via a single example.
In the example below, we have taken 4 values: Infinity
, the empty string ""
, undefined
, and NaN
. In the output, users see that the method returns the same for the Infinity
value.
For the undefined
, and NaN
value method returns the NaN
value, and for the empty string ""
, it returns 0.
let val = Infinity;
let ceilValue = Math.ceil(val);
console.log(ceilValue);
console.log(Math.ceil(NaN));
console.log(Math.ceil(""));
let num = undefined;
console.log(Math.ceil(num));
Output:
Infinity
NaN
0
NaN
The Math.ceil()
method is supported in all browsers.