Math.min
Returns the smallest of the given numbers.
Available since: ES1
Syntax
Math.min(value1, value2, /* … */)Parameters
| Parameter | Type | Description |
|---|---|---|
| value1, … | number | The numbers to compare. |
Return value
numberThe smallest of the numbers.
Examples
The smallest of several:
console.log(Math.min(4, 1, 9));Output:
1The minimum of an array — with spread:
const prices = [19, 7, 42];
console.log(Math.min(...prices));Output:
7Clamping a number to a range:
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
console.log(clamp(150, 0, 100));
console.log(clamp(-5, 0, 100));Output:
100
0Notes
Without arguments Math.min() returns Infinity, so the minimum of an empty array is Infinity too. Check the array for emptiness first.
See also
- Math.max — Returns the largest of the given numbers.
- Spread syntax — Three dots spread an array or object into separate items.
- Array.prototype.reduce — Reduces an array to a single value.
Related recipes
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.