Minimum selection
Choose the lowest value from several numeric candidates.
JavaScript · Function
Math.min() returns the smallest of zero or more supplied values. It returns Infinity with no arguments and NaN if any argument is or converts to NaN.
Math.min() compares all supplied arguments after converting them to numbers and returns the lowest result. With no arguments it returns Infinity; if any argument becomes NaN, the overall result is NaN. It is a static Math method, so call it as Math.min().
Math.min(value1)
Math.min(value1, value2)
Math.min(value1, value2, /* …, */ valueN)| Name | Type | Description |
|---|---|---|
value1, …, valueN | number | Zero or more numbers among which the lowest value will be selected and returned. |
The smallest of the given numbers; Infinity with no arguments, or NaN if any argument is or converts to NaN.
const values = [2, 3, 1];
const smallest = Math.min(...values);
const clipped = Math.min(100, 137);
console.log(smallest); // 1
console.log(clipped); // 100The spread syntax supplies the array elements as arguments; the second call limits the value to a maximum of 100.
Choose the lowest value from several numeric candidates.
Keep a computed value from exceeding a maximum.
Reduce an array to its smallest numeric element using spread syntax.
Math.min() returns the smallest supplied number. With zero arguments, it returns Infinity; if any argument is or converts to NaN, it returns NaN.
Follow-up: What happens with an array argument? Math.min(array) does not compare its elements; use Math.min(...array), while keeping argument-list limits in mind.
The function accepts zero or more values and selects the lowest numeric value. Values are converted to numbers as part of the operation, so conversions that produce NaN contaminate the result.
Follow-up: How would you safely handle untrusted values? Validate or explicitly convert them before calling Math.min(), then reject NaN rather than treating it as a valid bound.
Math.min() is static and belongs to the Math namespace, which is not a constructor. Invoke it as Math.min(...), not through an object created with new Math().
Follow-up: How can you find the minimum of an array? Spread the array into Math.min(...array), or use a loop for arbitrarily large input to avoid argument-length limits.
Math.max()Math.ceil()Math.floor()Math.round()Number.NaN