The Math.ceil()
method is a built-in function in JavaScript that returns the smallest integer greater than or equal to the given number.
This method takes one argument, which is the number to be rounded up to the nearest integer.
In this article, we will explore how to use the Math.ceil()
method in JavaScript with examples and code snippets.
Syntax:
Math.ceil(number);
The Math.ceil()
method takes one argument number
, which is the number to be rounded up to the nearest integer. The returned value will be the smallest integer greater than or equal to the given number.
Example 1: Using the Math.ceil()
method to round up a positive number:
let num = 4.3;
let rounded = Math.ceil(num);
console.log(rounded); // 5
In this example, we have a positive number 4.3
. We use the Math.ceil()
method to round up this number to the nearest integer, which is 5
. The result is then stored in the rounded
variable, which is then printed to the console.
Example 2: Using the Math.ceil()
method to round up a negative number:
let num = -4.3;
let rounded = Math.ceil(num);
console.log(rounded); // -4
In this example, we have a negative number -4.3
. We use the Math.ceil()
method to round up this number to the nearest integer, which is -4
. The result is then stored in the rounded
variable, which is then printed to the console.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.