What You'll Learn
You'll learn how to quickly find the largest or smallest number from a group of values using JavaScript's built-in Math.max() and Math.min() functions.
Finding the Extremes
Think of Math.max() and Math.min() as judges in a competition — they instantly pick the winner (highest) or the underdog (lowest) from any number of contestants.
Math.max()
Pass any number of arguments to Math.max(), and it returns the largest:
Math.max(5, 12, 8, 3); // 12
Math.max(-10, -5, -20); // -5
Math.max(42); // 42 (only contestant!)
Math.min()
Similarly, Math.min() returns the smallest value:
Math.min(5, 12, 8, 3); // 3
Math.min(-10, -5, -20); // -20
Math.min(42); // 42
Edge Cases
With no arguments, these methods return unexpected values that represent mathematical infinities:
Math.max(); // -Infinity (no max found)
Math.min(); // Infinity (no min found)
This happens because -Infinity is smaller than any number (so any actual value would beat it), and Infinity is larger than any number.
Common Use Cases
These functions shine when validating ranges, finding extremes in data, or clamping values:
// Find the highest score
const topScore = Math.max(87, 92, 78, 95); // 95
// Find the coldest temperature
const coldest = Math.min(-12, -5, -18, -3); // -18
Key Takeaway: Use Math.max() and Math.min() to quickly identify the largest or smallest value from multiple arguments without writing loops or comparisons.