Open In App

JavaScript Math.ceil( ) function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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));


Output

-2
-2

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));


Output

2
3

 Example 3: In this example, we have used Math.ceil function.

Javascript




console.log(Math.ceil("Geeksforgeeks"));       


Output

NaN

Example 4: In this example, we have used Math.ceil function.

Javascript




<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
console.log(Math.ceil(7+9));


Output

16

Supported Browsers:

  • Google Chrome 1 and above
  • Internet Explorer 12 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above


Last Updated : 08 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads