How to Use the Math.ceil() Method in JavaScript?

Estimated read time 1 min read

The Math.ceil() method in JavaScript rounds a number up to the nearest integer. For example:

let number = 3.14;
let rounded = Math.ceil(number);

console.log(rounded);

This will output 4, because 3.14 rounded up to the nearest integer is 4.

Here’s another example:

let number = -3.14;
let rounded = Math.ceil(number);

console.log(rounded);

This will output -3, because -3.14 rounded up to the nearest integer is -3.

Note that the Math.ceil() method always rounds a number up, even if it is already an integer. For example:

let number = 4;
let rounded = Math.ceil(number);

console.log(rounded);

This will still output 4, because 4 rounded up to the nearest integer is 4.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply