Math.ceil() function in JavaScript is used to round the number passed as a parameter to its nearest integer in an Upward direction of rounding i.e. towards the greater value.
Syntax:
Math.ceil(value);
Parameters:
This function accepts a single parameter as the number to be rounded to its nearest integer in the upward rounding method.
Return Value:
Result after rounding the number passed as a parameter to the function.
Examples:
Input : Math.ceil(4.23)
Output : 5
Input : Math.ceil(0.8)
Output : 1
Errors and Exceptions:
- A non-numeric string passed as a parameter returns NaN.
- An array with more than 1 integer passed as a parameter returns NaN.
- An empty variable passed as a parameter returns NaN.
- An empty string passed as a parameter returns NaN.
- An empty array passed as a parameter returns NaN.
Example 1: In this example, we have used Math.ceil function.
Javascript
<!-- NEGATIVE NUMBER EXAMPLE -->
console.log(Math.ceil(-2));
console.log(Math.ceil(-2.56));
|
Example 2: In this example, we have used Math.ceil function.
Javascript
<!-- POSITIVE NUMBER EXAMPLE -->
console.log(Math.ceil(2));
console.log(Math.ceil(2.56));
|
Example 3: In this example, we have used Math.ceil function.
Javascript
console.log(Math.ceil( "Geeksforgeeks" ));
|
Example 4: In this example, we have used Math.ceil function.
Javascript
<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
console.log(Math.ceil(7+9));
|
Supported Browsers:
- Google Chrome 1 and above
- Internet Explorer 12 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
08 Dec, 2023
Like Article
Save Article