Array Max & Min in JavaScript
Find the max and min of an array in JavaScript with Math.max(...arr), a loop, reduce and sort. Why empty arrays give -Infinity. Explanation and output.
1// Method 1: Using Math.max and Math.min with spread operator2let numbers = [10, 5, 20, 15, 8, 25];34let max = Math.max(...numbers);5let min = Math.min(...numbers);67console.log("Array:", numbers);8console.log("Maximum:", max);9console.log("Minimum:", min);1011// Method 2: Using for loop12function findMaxMin(arr) {13 if (arr.length === 0) return { max: null, min: null };1415 let max = arr[0];16 let min = arr[0];1718 for (let i = 1; i < arr.length; i++) {19 if (arr[i] > max) max = arr[i];20 if (arr[i] < min) min = arr[i];21 }2223 return { max, min };24}2526let result = findMaxMin([10, 5, 20, 15, 8]);27console.log("\nUsing loop:", result);2829// Method 3: Using reduce30function findMaxMinReduce(arr) {31 return arr.reduce((acc, num) => {32 acc.max = Math.max(acc.max, num);33 acc.min = Math.min(acc.min, num);34 return acc;35 }, { max: arr[0], min: arr[0] });36}3738console.log("\nUsing reduce:", findMaxMinReduce([3, 7, 2, 9, 1]));3940// Method 4: Using sort41function findMaxMinSort(arr) {42 let sorted = [...arr].sort((a, b) => a - b);43 return {44 min: sorted[0],45 max: sorted[sorted.length - 1]46 };47}4849console.log("\nUsing sort:", findMaxMinSort([12, 4, 8, 19, 3]));
Sample Output
Array: [ 10, 5, 20, 15, 8, 25 ]
Maximum: 25
Minimum: 5
Using loop: { max: 20, min: 5 }
Using reduce: { max: 9, min: 1 }
Using sort: { min: 3, max: 19 }In this one
- How spread passes array items to Math.max and Math.min
- Why empty arrays give -Infinity and how to guard
- How to track max and min in one loop
- Why sort needs a compare function for numbers
How it works
- 1
Math.max(...numbers) returns 25 and Math.min returns 5.
- 2
findMaxMin loops through [10, 5, 20, 15, 8] and returns max 20, min 5.
- 3
reduce keeps a running { max, min } and returns 9 and 1.
- 4
Sorting a copy gives min 3 and max 19.
Edge cases
Empty array with spread
Math.max(...[]) returns -Infinity.
Check arr.length === 0 first and return null or a message.
Sort without a compare function
[10, 9, 100].sort() gives [10, 100, 9].
Default sort compares as text. Use (a, b) => a - b for numbers.
Very large arrays
Spreading 200,000+ items can throw a RangeError.
Each item becomes an argument, and there is a limit. Use the loop or reduce for huge arrays.
What's going on
This program finds the largest and smallest values in an array. An array is an ordered list of values, like the daily attendance counts of a class for one week.
Method 1 is the short one: Math.max(...numbers) and Math.min(...numbers). Math.max does not accept an array directly. The spread operator ... unpacks the array into separate arguments, so Math.max(...[10, 5, 20]) becomes Math.max(10, 5, 20).
Be careful with empty arrays. Do not spread an empty array into Math.max. Math.max() with no arguments returns -Infinity, and Math.min() returns Infinity. Your program will print those strange values instead of an error. Check arr.length first.
Method 2 is the loop you could write on a whiteboard. It starts with max and min both set to the first item, then walks the rest of the array. If an item is bigger than max, it becomes the new max. If it is smaller than min, it becomes the new min. It returns { max: null, min: null } for an empty array, which is a clear signal that there was nothing to compare.
Method 3 uses reduce to carry an object with max and min through the array. Method 4 sorts a copy with (a, b) => a - b and takes the first and last items. Sorting does more work than needed, but it is easy to read. The copy [...arr] keeps the original order safe.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does Math.max() return with no arguments?
Pick an answer to check it.
Question 2
What does ... do in Math.max(...arr)?
Pick an answer to check it.
Questions people ask
Why does the loop start at index 1?
max and min already hold arr[0], so comparing it again adds nothing.
Does Method 4 change the original array?
No. [...arr] makes a copy before sorting.
What if the array has strings?
Math.max tries to convert them. "abc" becomes NaN and the result is NaN.