Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Math.max() / min() / abs() / random()

Math.max() / min() / abs() / random()

Methods for comparing numbers and performing mathematical calculations. You can find maximum and minimum values, generate random numbers, and compute absolute values, powers, and square roots.

Syntax

// Returns the largest value.
var num = Math.max(value1, value2, ...);

// Returns the smallest value.
var num = Math.min(value1, value2, ...);

// Returns a random number between 0 (inclusive) and 1 (exclusive).
var num = Math.random();

// Returns the absolute value.
var num = Math.abs(number);

// Returns the result of raising base to the power of exponent.
var num = Math.pow(base, exponent);

// Returns the square root.
var num = Math.sqrt(number);

Method List

MethodDescription
Math.max(val1, val2, ...)Returns the largest number among the arguments. Returns -Infinity if no arguments are provided.
Math.min(val1, val2, ...)Returns the smallest number among the arguments. Returns Infinity if no arguments are provided.
Math.random()Returns a pseudo-random number in the range [0, 1). Takes no arguments.
Math.abs(number)Returns the absolute value of a number. Negative numbers are converted to positive; positive numbers are returned as-is.
Math.pow(base, exponent)Returns the value of base raised to the power of exponent. In ES6 and later, the ** operator performs the same calculation.
Math.sqrt(number)Returns the square root of a number. Returns NaN if a negative number is passed.

Sample Code

// Use Math.max() and Math.min() to find the largest and smallest values.
console.log(Math.max(10, 25, 3)); // Outputs '25'.
console.log(Math.min(10, 25, 3)); // Outputs '3'.

// Use the spread syntax to find the max/min of an array.
var scores = [85, 92, 78, 96, 88];
console.log(Math.max(...scores)); // Outputs '96'.
console.log(Math.min(...scores)); // Outputs '78'.

// Use Math.random() to generate a random number.
console.log(Math.random()); // Outputs a random number between 0 (inclusive) and 1 (exclusive).

// Practical example: simulating a six-sided dice roll.
var dice = Math.floor(Math.random() * 6) + 1;
console.log(dice); // Outputs a random integer from 1 to 6.

// A function that generates a random integer within a specified range.
function getRandomInt(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(10, 20)); // Outputs a random integer from 10 to 20.

// Use Math.abs() to get the absolute value.
console.log(Math.abs(-15)); // Outputs '15'.
console.log(Math.abs(15));  // Outputs '15'.

// Practical example: finding the difference between two points.
var a = 30;
var b = 75;
console.log(Math.abs(a - b)); // Outputs '45'. The result is always positive regardless of which value is larger.

// Use Math.pow() to compute a power.
console.log(Math.pow(2, 10)); // Outputs '1024'. This is 2 to the 10th power.
console.log(2 ** 10);         // Outputs '1024'. The ES6 exponentiation operator gives the same result.

// Use Math.sqrt() to compute a square root.
console.log(Math.sqrt(144)); // Outputs '12'.
console.log(Math.sqrt(2));   // Outputs '1.4142135623730951'.

Overview

The Math object is a built-in object that provides mathematical constants and functions. It is not a constructor, so you never write new Math(). Instead, you call all of its methods directly, like Math.max().

Math.random() returns a random number between 0 (inclusive) and 1 (exclusive). Because that raw value is rarely useful on its own, it is commonly combined with Math.floor() to produce an integer. To generate a random integer from 1 to N, use the pattern Math.floor(Math.random() * N) + 1. Note that Math.random() is not cryptographically secure, so it is not suitable for security-sensitive purposes such as password generation.

Math.pow() can be replaced by the exponentiation operator ** introduced in ES6. The Math object also provides useful constants such as Math.PI (the ratio of a circle's circumference to its diameter) and Math.E (the base of natural logarithms).

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
Supported in all versions
Edge Edge
80+
11 or earlier ×
IE IE
11+
2 or earlier ×
Opera Opera
48+
2 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .