comparison
Math.min
Returns the smallest of zero or more numbers passed as separate arguments. With no arguments returns +Infinity.
Math.min(...values) Parameters & edge cases
| Case | Behavior |
|---|---|
| ...values | Any number of numeric arguments (0 or more) |
| no args | Math.min() returns +Infinity (the identity for the min operation) |
| NaN input | Any NaN makes the result NaN |
| clamp idiom | Math.min(max, Math.max(min, x)) clamps x into [min, max] |
Examples
console.log(Math.min(1, 2, 3)); // 1 Straightforward variadic min.
console.log(Math.min()); // Infinity No args returns +Infinity.
console.log(Math.min(...[3, 1, 2])); // 1 Spread an array to pass its items as arguments.
console.log(Math.min(Math.max(0, x), 100)); // clamp x to [0,100] The canonical clamp idiom.
Gotcha
As with Math.max, spreading giant arrays can overflow the argument stack. Consider a reduce loop for arrays larger than ~100k items.
Related
Math.max
Returns the largest of zero or more numbers passed as separate arguments. With no arguments returns -Infinity.
Math.abs
Returns the absolute value of a number — its distance from zero without regard to sign. Useful for differences, magnitudes, and tolerance checks.
Math.sign
Returns the sign of a number as 1, -1, 0, -0, or NaN. Introduced in ES2015 as a compact replacement for the ternary `x > 0 ? 1 : x < 0 ? -1 : 0`.