HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers. Unlike other objects, the Math object has no constructor. All properties and methods of Math are static.


Math Properties (Constants)

JavaScript provides several mathematical constants that you can access via the Math object.

  • Math.PI — Approx. 3.14159
  • Math.E — Euler's number
  • Math.SQRT2 — Square root of 2
  • Math.LN10 — Natural logarithm of 10

Rounding Methods

There are 4 common ways to round a number to an integer:

Method Description
Math.round(x) Returns x rounded to its nearest integer
Math.ceil(x) Returns x rounded up to its nearest integer
Math.floor(x) Returns x rounded down to its nearest integer
Math.trunc(x) Returns the integer part of x (removes decimals)

Other Common Math Methods

Math.pow(x, y)

Returns the value of x to the power of y.

Math.sqrt(x)

Returns the square root of x.

Math.abs(x)

Returns the absolute (positive) value of x.

Math.min/max()

Finds the lowest or highest value in a list.


Math.sign()

This method checks if a number is negative, null, or positive. It returns -1 (negative), 0 (null), or 1 (positive).

Math.sign(-4); // returns -1
Math.sign(0);  // returns 0
Math.sign(4);  // returns 1

Key Points to Remember

  • The Math object is static—no new Math() is required
  • Use Math.floor() for reliable "downwards" rounding
  • Math.PI is the most common constant for geometry calculations
  • Math.min() and Math.max() can take any number of arguments
  • Math.trunc() is safer for simply removing decimals than rounding
  • Mathematical functions help in creating graphics, physics simulations, and data analysis