JavaScript Math Object

๐Ÿ“˜ What is Math Object?

The Math object allows you to perform mathematical tasks, like rounding numbers, generating random numbers, and performing power calculations. It is built-in and does not require creating an instance.

๐Ÿงฎ Common Math Methods

  • Math.round() - rounds to nearest integer
  • Math.ceil() - rounds up
  • Math.floor() - rounds down
  • Math.min() - returns minimum value
  • Math.max() - returns maximum value
  • Math.pow() - power of a number
  • Math.random() - random number between 0 and 1
  • Math.trunc() - removes decimal part [ES6]

๐Ÿ’ป Examples

console.log(Math.round(4.6));   // 5
console.log(Math.ceil(4.1));    // 5
console.log(Math.floor(4.9));   // 4
console.log(Math.min(1, 3, 0)); // 0
console.log(Math.max(1, 3, 0)); // 3
console.log(Math.pow(2, 3));    // 8
console.log(Math.random());      // 0-1
console.log(Math.trunc(4.7));   // 4
            

๐Ÿงช Try It Yourself

Click the button to see Math methods in action: