JavaScript’s Math is a built-in global object that provides mathematical constants and helper methods. It is not a constructor, so you do not create it with new Math(). All methods are used in the form Math.xxx().

Properties

Common constants include:

  • Math.PI
  • Math.E
  • Math.LN2
  • Math.SQRT2
console.log(Math.PI); // 3.141592653589793

Methods

1. Rounding methods

Math.floor(4.9); // 4
Math.ceil(4.1); // 5
Math.round(4.5); // 5
Math.trunc(4.9); // 4

2. Maximum and minimum

Math.max(1, 5, 3); // 5
Math.min(1, 5, 3); // 1

You can also combine them with spread syntax:

const nums = [4, 2, 9, 7];
Math.max(...nums); // 9

3. Math calculation

Math.abs(-10); // 10
Math.pow(2, 3); // 8
Math.sqrt(16); // 4
Math.cbrt(27); // 3

Many modern codebases prefer ** over Math.pow:

2 ** 3; // 8

4. Random numbers

Math.random(); // 0 <= n < 1

Random integer in a range:

function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

5. Trigonometric functions

Math.sin(Math.PI / 2); // 1
Math.cos(0); // 1
Math.tan(0); // 0

Remember that JavaScript trig functions use radians, not degrees.

Summary

Math is a lightweight but essential utility object. The most commonly used parts in everyday development are:

  • rounding
  • min/max
  • absolute value
  • square roots
  • random numbers