Course contentsShow
← JavaScript
Lesson 138 of 2,7234. Working with NumbersFree lesson

Math.min and Math.max with Multiple Arguments

Finding minimum and maximum values from a set of numbers, including spread operator usage.

Math.min and Math.max with Multiple Arguments

You'll learn how to find the smallest or largest value from a set of numbers using JavaScript's built-in Math methods.

The Core Idea

Math.min() and Math.max() are static methods that can accept any number of arguments and return the smallest or largest value, respectively. Think of them like a referee at a contest—Math.min() picks the "winner" with the lowest score, while Math.max() picks the one with the highest.

Basic Usage

Math.min(5, 2, 9, 1);     // Returns 1
Math.max(5, 2, 9, 1);     // Returns 9

Math.min(42);             // Returns 42 (only one value)
Math.max(-10, -5, -20);   // Returns -5 (least negative)

You can pass as many arguments as you need—two, ten, or even hundreds.

Edge Cases

Math.min();               // Returns Infinity (no arguments)
Math.max();               // Returns -Infinity (no arguments)

Math.min(5, NaN, 3);      // Returns NaN (any NaN makes result NaN)
Math.max(5, Infinity);    // Returns Infinity

Using with Arrays (Spread Operator)

If your numbers are in an array, you can "spread" them into individual arguments:

const scores = [45, 23, 67, 12];

Math.min(...scores);      // Returns 12
Math.max(...scores);      // Returns 67

The spread operator (...) unpacks the array elements as separate arguments, like converting [45, 23, 67, 12] into 45, 23, 67, 12.

Real-World Example

const temperatures = [22, 18, 25, 19, 30];
const coldest = Math.min(...temperatures);  // 18
const hottest = Math.max(...temperatures);  // 30

Key Takeaway: Math.min() and Math.max() can take unlimited arguments to find extreme values, and the spread operator lets you work with arrays seamlessly.