SyntaxStudy
Sign Up
Home JavaScript Reference Math.max() / Math.min()

Math.max() / Math.min()

method

Returns the largest or smallest of zero or more numbers.

Syntax

Math.max(...values) Math.min(...values)

Example

javascript
Math.max(1, 5, 3, 9, 2); // 9
Math.min(1, 5, 3, 9, 2); // 1

// With array (use spread)
const scores = [82, 95, 61, 78, 90];
console.log(Math.max(...scores)); // 95
console.log(Math.min(...scores)); // 61

// Clamp a value
const clamp = (val, min, max) => Math.min(Math.max(val, min), max);
clamp(150, 0, 100); // 100
clamp(-5, 0, 100);  // 0