JavaScript Math Object: Complete Guide

Learn the JavaScript Math object and its most useful methods for calculations, rounding, random values, and mathematical constants.

5 min read

The JavaScript Math object is a built-in collection of mathematical constants and functions. You do not create a Math instance. You call its methods directly: Math.round(3.7), Math.random(), Math.max(5, 10).

Math is always available without importing anything. It works in browsers and Node.js identically.

javascriptjavascript
console.log(Math.PI);
console.log(Math.round(3.7));
console.log(Math.max(5, 12, 3));
console.log(Math.random());

This prints approximately 3.141592653589793, then 4, then 12, then a random decimal like 0.7342. Each call stands alone because every Math method returns a new value.

Rounding methods

Math provides three rounding methods. They differ in the direction they round when the number has a fractional part.

javascriptjavascript
console.log(Math.round(3.7));
console.log(Math.floor(3.7));
console.log(Math.ceil(3.7));

Math.round prints 4 because 3.7 is closer to 4 than to 3. Math.floor prints 3 because it always rounds down. Math.ceil prints 4 because it always rounds up.

The behavior with negative numbers is less obvious:

javascriptjavascript
console.log(Math.floor(-2.3));
console.log(Math.ceil(-2.3));
console.log(Math.round(-2.3));

Math.floor prints -3 because "down" on the number line means more negative. Math.ceil prints -2 because "up" means less negative. Math.round prints -2 because -2.3 is closer to -2 than to -3.

For a deeper comparison of all three, see the guide to rounding numbers with floor, ceil, and round.

Random numbers

Math.random() returns a decimal between 0 (inclusive) and 1 (exclusive). Every call produces a different value.

javascriptjavascript
console.log(Math.random());
console.log(Math.random());

Two calls print two different decimals, each between 0 and just under 1. To get a random integer in a range, combine Math.random with Math.floor:

javascriptjavascript
function rollDie() {
  return Math.floor(Math.random() * 6) + 1;
}
 
console.log(rollDie());

This prints a number from 1 to 6. Multiplying by 6 gives a range of 0 to 5.999..., floor brings it to 0 through 5, and adding 1 shifts to 1 through 6.

For more patterns including random array elements and seeded generation caveats, see the guide to random number generation.

Min, max, and absolute value

Math.max and Math.min find the largest or smallest value from any number of arguments. They accept individual numbers, not arrays.

javascriptjavascript
console.log(Math.max(5, 12, 3, 8));
console.log(Math.min(5, 12, 3, 8));

The first prints 12, the largest. The second prints 3, the smallest.

To find the max or min of array values, spread the array into individual arguments with the spread operator:

javascriptjavascript
const scores = [88, 92, 75, 96];
console.log(Math.max(...scores));

This prints 96, the highest score in the array. The spread operator unpacks the array into separate arguments so Math.max can compare them individually, since Math.max never accepts an array directly.

Math.abs returns the absolute value of a number, stripping any negative sign so the result is always zero or positive:

javascriptjavascript
console.log(Math.abs(-42));
console.log(Math.abs(42));

Both print 42, since Math.abs treats -42 and 42 identically once the sign is removed.

Powers, roots, and constants

Math.pow raises a number to a power, and Math.sqrt returns its square root. These cover the most common exponent and root calculations you will need for geometry and scaling.

javascriptjavascript
console.log(Math.pow(2, 10));
console.log(Math.sqrt(144));

The first prints 1024 (2^10). The second prints 12.

Common mathematical constants are available as properties:

ConstantValue
Math.PI3.141592653589793
Math.E2.718281828459045
Math.SQRT21.4142135623730951

Use Math.PI for circle calculations:

javascriptjavascript
const radius = 5;
const area = Math.PI * Math.pow(radius, 2);
 
console.log(area);

This prints approximately 78.54, the area of a circle with radius 5.

Common mistakes

Trying to create a Math instance with new Math() throws a TypeError. Math is not a constructor. Call its methods directly.

Passing an array to Math.max without spreading returns NaN. The expression Math.max([1, 2, 3]) does not work because max expects individual numeric arguments. Use Math.max(...arr) instead.

Expecting Math.random to be cryptographically secure is incorrect. Math.random is suitable for games and UI randomness but not for security tokens or password generation. Use crypto.getRandomValues for cryptographic needs.

Rune AI

Rune AI

Key Insights

  • Math is a built-in object with static methods. No constructor needed.
  • Math.round, Math.floor, and Math.ceil are the three main rounding methods.
  • Math.random() returns a decimal between 0 (inclusive) and 1 (exclusive).
  • Math.max and Math.min find the largest or smallest value from arguments.
  • Math.PI and Math.E provide common mathematical constants.
RunePowered by Rune AI

Frequently Asked Questions

Do I need to create a new Math object?

No. Math is a built-in object with static methods and properties. You call them directly: Math.round(3.7), never new Math(). Math is not a constructor.

What is the difference between Math.round and Math.floor?

Math.round rounds to the nearest integer, with .5 rounding up. Math.floor always rounds down to the nearest integer. Math.floor(3.9) is 3, while Math.round(3.9) is 4.

Can I use Math methods on BigInt values?

No. Math methods only work with the Number type. BigInt values must be converted to Number first, which may lose precision for very large integers.

Conclusion

The Math object provides mathematical constants like Math.PI and methods for rounding, random numbers, min/max, powers, and trigonometry. It is always available without importing anything. Remember that Math methods return new values and never modify their inputs.