Min/Max & Random Methods
These methods handle comparisons and random number generation.
max(...values)
Returns the largest of the given numbers.
Example:
console.log(Math.max(1, 5, 2)); // 5
min(...values)
Returns the smallest of the given numbers.
Example:
console.log(Math.min(1, 5, 2)); // 1
random()
Returns a random number between 0
(inclusive) and 1
(exclusive).
Example:
console.log(Math.random()); // e.g., 0.723942837
Random integer in range:
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(1, 10)); // e.g., 7
Other Methods
Other Methods
These methods provide specialized functionality.
clz32(x)
Returns the number of leading zero bits in the 32-bit integer representation of x
.
Example:
console.log(Math.clz32(1)); // 31
imul(x, y)
Returns the result of 32-bit integer multiplication of x
and y
.
Example:
console.log(Math.imul(2, 4)); // 8
hypot(...values)
Returns the square root of the sum of squares of its arguments (Euclidean distance).
Example:
let x = 3, y = 4;
console.log(Math.hypot(x, y)); // 5