totn JavaScript

JavaScript: Math ceil() function

This JavaScript tutorial explains how to use the math function called ceil() with syntax and examples.

Description

In JavaScript, ceil() is a function that is used to return the smallest integer value that is greater than or equal to a number. In other words, the ceil() function rounds a number up and returns an integer value. Because the ceil() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Syntax

In JavaScript, the syntax for the ceil() function is:

Math.ceil(number);

Parameters or Arguments

number
The number used to find the smallest integer.

Returns

The ceil() function returns the smallest integer value that is greater than or equal to a number.

Note

  • Math is a placeholder object that contains mathematical functions and constants of which ceil() is one of these functions.

Example

Let's take a look at an example of how to use the ceil() function in JavaScript.

For example:

console.log(Math.ceil(32.65));
console.log(Math.ceil(8.1));
console.log(Math.ceil(-4.2));

In this example, we have invoked the ceil() function using the Math class.

We have written the output of the ceil() function to the web browser console log, for demonstration purposes, to show what the ceil() function returns.

The following will be output to the web browser console log:

33
9
-4

In this example, the first output to the console log returned 33 which is 32.65 rounded up to an integer value.

The second output to the console log returned 9 which is 8.1 rounded up to an integer value.

The third output to the console log returned -4 which is -4.2 rounded up to an integer value. Notice that when dealing with negative numbers, the ceil() function rounds up towards zero and in this case, returned -4 and not -5.