JavaScript Math Object

The Math object provides mathematical constants and functions for performing mathematical operations. Unlike Date or Array, Math is not a constructor—you cannot create new Math objects. All Math properties and methods are static, accessed directly as Math.methodName().

Math Methods

Math.round() rounds a number to the nearest integer using standard rounding rules: .5 and above rounds up, below .5 rounds down. Math.round(4.7) returns 5, Math.round(4.4) returns 4, and Math.round(4.5) returns 5. This is the most commonly used rounding method for general-purpose rounding where you want the closest integer.

Math.ceil() (ceiling) always rounds up to the next integer, regardless of the decimal portion. Math.ceil(4.1) returns 5, and Math.ceil(4.9) also returns 5. Even Math.ceil(4.01) returns 5. This method is useful when you need to ensure you have "enough" of something—like calculating the number of boxes needed to hold items.

Math.floor() always rounds down to the previous integer, discarding the decimal portion. Math.floor(4.9) returns 4, and Math.floor(4.1) also returns 4. floor() is commonly used for converting to integers when you want to round down, and it's essential for generating random integers: Math.floor(Math.random() * max).

Math.random() generates a pseudo-random decimal number between 0 (inclusive) and 1 (exclusive). To get random integers in a range, use Math.floor(Math.random() * (max - min + 1)) + min. For example, random 1-6 (dice): Math.floor(Math.random() * 6) + 1. Note that Math.random() is not cryptographically secure—use crypto.getRandomValues() for security-sensitive randomness.

Math.max() returns the largest of zero or more numbers. Math.max(1, 5, 3, 9, 2) returns 9. You can use the spread operator to find the max in an array: Math.max(...array). If any argument is NaN, the result is NaN. If no arguments are provided, it returns -Infinity. max() is useful for clamping values or finding extremes.

Math.min() returns the smallest of zero or more numbers. Math.min(1, 5, 3, 9, 2) returns 1. Like max(), you can use it with arrays via spread. With no arguments, it returns Infinity. Together, Math.min() and Math.max() are useful for value validation, clamping numbers to ranges, or finding extremes in datasets.

// Rounding
console.log(Math.round(4.7));  // 5
console.log(Math.round(4.4));  // 4
console.log(Math.ceil(4.1));   // 5
console.log(Math.floor(4.9));  // 4

// Random
console.log(Math.random());    // 0 to 1
const randomInt = Math.floor(Math.random() * 10); // 0 to 9

// Min/Max
console.log(Math.max(1, 5, 3, 9, 2));  // 9
console.log(Math.min(1, 5, 3, 9, 2));  // 1

// Power and Square Root
console.log(Math.pow(2, 3));   // 8
console.log(Math.sqrt(16));    // 4

Math Constants

Math.PI represents the mathematical constant π (pi), approximately 3.141592653589793. It's the ratio of a circle's circumference to its diameter. Math.PI is essential for circle and sphere calculations: circumference = 2 * Math.PI * radius, area = Math.PI * radius². Using Math.PI is more accurate and readable than writing 3.14 or 3.14159 manually.

Math.E represents Euler's number e, approximately 2.718281828459045. It's the base of natural logarithms and appears frequently in exponential growth/decay calculations, compound interest, probability, and calculus. While less commonly used in everyday programming than PI, it's essential for scientific and mathematical applications.

Math.SQRT2 is the square root of 2, approximately 1.4142135623730951. While you could calculate this with Math.sqrt(2), having it as a constant is slightly more efficient and makes code clearer when you're working with right triangles, unit circles, or geometric calculations that frequently use √2.

Math.LN2 is the natural logarithm of 2, approximately 0.6931471805599453. It's used in mathematical calculations involving logarithms and exponential functions. Like SQRT2, it's provided as a constant for efficiency and clarity rather than calculating Math.log(2) repeatedly. Other math constants include LN10, LOG2E, LOG10E, and SQRT1_2.

All Math constants are read-only properties. Attempting to change them has no effect (in strict mode, it throws an error). Math.PI = 3 won't actually change PI—it will either fail silently or throw an error. These constants maintain their precise values regardless of your code, ensuring mathematical accuracy.

// Constants
console.log(Math.PI);      // 3.141592653589793
console.log(Math.E);       // 2.718281828459045
console.log(Math.SQRT2);   // 1.4142135623730951

// Using constants
const radius = 5;
const area = Math.PI * Math.pow(radius, 2);
console.log(area); // 78.53981633974483

// Absolute value
console.log(Math.abs(-5));  // 5