The JavaScript Math.min() method
The Math.min() method returns the smallest of the numbers you pass it. Math.min(3, 8, 5) is 3. It takes separate arguments, so spread an array to find its minimum: Math.min(...numbers). With no arguments it returns Infinity. Its opposite is Math.max().
Overview
Math.min() compares the numbers you give it and returns the smallest. As with its twin Math.max(), it takes values as separate arguments, not an array — Math.min(3, 8, 5), not Math.min([3, 8, 5]). Pass an array directly and you get NaN.
The fix is the spread operator: Math.min(...numbers) spreads the array into individual arguments, the standard way to find an array's minimum. Together Math.min() and Math.max() are also the building blocks of clamping a value to a range: Math.min(max, Math.max(min, value)) keeps value between min and max.
Two edge cases mirror Math.max(): with no arguments at all, Math.min() returns Infinity (its identity), so guard against empty arrays; and any non-numeric argument makes the whole result NaN.
Syntax
Math.min(3, 8, 5) // 3
Math.min(...[3, 8, 5]) // 3 (spread an array)
// clamp value between min and max
Math.min(max, Math.max(min, value))
Parameters
The Math.min() method accepts the following parameters.
| Parameter | Description |
|---|---|
value1 ... valueN |
The numbers to compare. Pass them as separate arguments — spread an array with ... to use its elements. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const temps = [18, 7, 24, 11];
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
document.getElementById('out').textContent =
'min: ' + Math.min(...temps) + '\n' +
'clamp(30, 0, 25): ' + clamp(30, 0, 25); // min: 7 / clamp: 25
</script>
Best practices
- Spread arrays into it:
Math.min(...arr)— passing the array directly returnsNaN. - Guard against empty input, since
Math.min()with no arguments returnsInfinity. - Clamp a value with
Math.min(max, Math.max(min, value)). - Use Math.max() the same way for the largest value.
Frequently asked questions
How do I find the smallest number in an array?
Math.min(): Math.min(...numbers).Why does Math.min() return NaN?
NaN.What does Math.min() return with no arguments?
Infinity. So check for an empty array before spreading it in.How do I clamp a number to a range?
Math.min(max, Math.max(min, value)) keeps value within [min, max].