Math.min()
ES1+Returns the smallest of the zero or more numbers given as input parameters.
Syntax
Math.min(value1, value2, ...)Parameters
values number Zero or more numbers
Return Value
The smallest of the given numbers, or Infinity if no arguments
Examples
console.log(Math.min(1, 3, 2));
console.log(Math.min(-1, -3, -2));
console.log(Math.min(...[4, 5, 6])); 📌 When to Use
Use Math.min() for finding lowest values, setting lower bounds, clamping values, or determining the minimum resource needed.
⚠️ Common Mistakes
Forgetting that Math.min() with no arguments returns Infinity, not 0.
Not handling NaN in the input array - if any value is NaN, the result is NaN.
✅ Best Practices
Combine Math.min() and Math.max() to clamp values: Math.max(min, Math.min(max, value)).
Filter out NaN values before using Math.min(): arr.filter(n => !isNaN(n)).
⚡ Performance Notes
Same performance characteristics as Math.max(). For very large arrays, use reduce() with an initial value of Infinity.
🌍 Real World Example
Progress Bar with Bounds
Calculate progress percentage with minimum and maximum constraints for smooth animation.
function clampProgress(current, total, options = {}) {
const { minPercent = 0, maxPercent = 100 } = options;
const rawPercent = (current / total) * 100;
// Clamp between min and max
const clampedPercent = Math.max(
minPercent,
Math.min(maxPercent, rawPercent)
);
return {
percent: clampedPercent,
display: clampedPercent.toFixed(1) + '%',
isComplete: clampedPercent >= maxPercent
};
}
console.log(clampProgress(75, 100)); // { percent: 75, display: '75.0%', isComplete: false }
console.log(clampProgress(150, 100)); // { percent: 100, display: '100.0%', isComplete: true }