JavaScript Math Object

The Math object is a global, built-in utility object that provides constants and functions for performing advanced mathematical tasks. Unlike the String or Number methods, you never need to create a new Math() object; all its properties and methods are static and can be used directly.

Key Fact: The Math object works only with the Number data type and does not operate on BigInt.

The Math object provides common mathematical constants as readily accessible properties.

  • Math.PI: The ratio of a circle’s circumference to its diameter (approximately 3.14159).
  • Math.E: Euler’s number, the base of the natural logarithm (approximately 2.718).
// Math Object Properties
console.log(Math.PI); // Output: 3.141592653589793
console.log(Math.E); // Output: 2.718281828459045

// If a circle is 10 inches wide, the diameter is 5 inches (*2).
// 2 * 5 * PI
console.log(2 * 5 * Math.PI); // Output: 31.41592653589793
JavaScript

For 99% of general web development (building websites, apps, dashboards), you will likely never use any property other than Math.PI, but there are also “Root” and “Logarithm” properties for square root and advanced mathematic calculations.

These methods are crucial for controlling decimal precision by rounding a number according to specific rules. They always return an integer (whole number).

  • Math.round(x): Returns the value of a number rounded to the nearest integer. Standard rounding rules apply (0.5 and above rounds up).
  • Math.floor(x): Returns the largest integer less than or equal to a given number (always rounds down).
  • Math.ceil(x): Returns the smallest integer greater than or equal to a given number (always rounds up).
  • Math.trunc(x): Truncates the number, returning only the integer part by removing any fractional digits. This method rounds towards zero.
  • Math.sign(x): Returns 1 if the number is positive, -1 if it is negative, and 0 if it is zero.
// Math.round(): Standard rounding
console.log("Math.round");
console.log(Math.round(5.7)); // Output: 6
console.log(Math.round(5.5)); // Output: 6
console.log(Math.round(5.4)); // Output: 5

// Math.floor(): Always rounds down
console.log("Math.floor");
console.log(Math.floor(5.7)); // Output: 5
console.log(Math.floor(-5.7)); // Output: -6

// Math.ceil(): Always rounds up
console.log("Math.ceil");
console.log(Math.ceil(5.7)); // Output: 6
console.log(Math.ceil(-5.4)); // Output: -5

// Math.trunc(): Returns the integer (whole number)
console.log("Math.trunc");
console.log(Math.trunc(5.7)); // Output: 5
console.log(Math.trunc(-5.4)); // Output: -5

// Math.sign(): Determine the "direction" of a number
console.log("Math.sign");
console.log(Math.sign(50)); // Output: 1
console.log(Math.sign(-25)); // Output: -1
console.log(Math.sign(0)); // Output: 0
JavaScript

These methods find the highest or lowest value from a list of numbers.

  • Math.max(a, b, c, ...): Returns the largest of zero or more numbers.
  • Math.min(a, b, c, ...): Returns the smallest of zero or more numbers.
// Find the highest number with Math.max
console.log(Math.max(85, 92, 78, 100, 88)); // Output: 100

// Find the lowest number with Math.min
console.log(Math.min(10, -5, 20, 0)); // Output: -5

// Passing an array to Math.max() with "..." spread operator:
const scores = [45, 128, 12, 199, 56];
console.log(Math.max(...scores)); // Output: 199
JavaScript

The Math.random() method is arguably the most common and is essential for tasks like generating unique IDs, dealing cards, or simulating events.

  • Math.random(): Returns a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
// Generates a random number between 0 and 0.999..
console.log(Math.random()); // Output: 0.23456..
JavaScript

To get a useful random whole number within a specific range, you must combine Math.random() with Math.floor(). Generate a random integer between 1 and 10 with:

// Formula for range [Min, Max]
const min = 1;
const max = 10;

// Step 1: Math.random() gives [0, 1)
// Step 2: * (max - min + 1) expands the range to [0, 10)
// Step 3: Math.floor() rounds down to an integer [0, 9]
// Step 4: + min shifts the range to [1, 10]
const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;

console.log(randomNumber); // Output: Random number between 1 and 10
JavaScript
  • Math.pow(base, exponent): Returns the value of the base raised to the power of the exponent (this is the same as the ** exponentiation operator).
  • Math.sqrt(x): Returns the square root of a number.
  • Math.abs(x): Returns the absolute (non-negative) value of a number.
// Math.pow() (equivalent to the ** operator)
console.log(Math.pow(2, 3)); // Output: 8 (2 * 2 * 2)
console.log(Math.pow(10, 4)); // Output: 10000 (10 * 10 * 10 * 10)

// Math.sqrt()
console.log(Math.sqrt(25)); // Output: 5
console.log(Math.sqrt(49)); // Output: 7

// Math.abs()
console.log(Math.abs(-99)); // Output: 99
console.log(Math.abs(10)); // Output: 10 (no change because it's positive)
JavaScript

While this overview covers the most common Math methods, you can refer to the Math object in your console with console.log(Math); for a complete list of every built-in property and method available for further experimentation.