How to find the maximum value in an array in JavaScript

Finding the maximum value in numeric arrays is essential for data analysis, creating charts, determining price ranges, and implementing features like progress bars or statistical displays in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented maximum value calculations in components like dashboard charts, pricing tables, and analytics widgets where identifying peak values is crucial for data visualization. From my extensive expertise, the most elegant and efficient solution is using Math.max() combined with the spread operator to find the largest number. This approach is concise, readable, and leverages JavaScript’s built-in mathematical functions for optimal performance.

Use Math.max() with the spread operator to find the largest value in an array.

const numbers = [3, 7, 2, 9, 1, 5]
const maximum = Math.max(...numbers)
// Result: 9

The Math.max() function finds the largest of the provided arguments, and the spread operator ... expands the array elements as individual arguments to the function. In this example, Math.max(...numbers) is equivalent to Math.max(3, 7, 2, 9, 1, 5), which returns 9 as the largest value. This method only works with numeric arrays and will return NaN if any element cannot be converted to a number. For empty arrays, it returns -Infinity.

Best Practice Note:

This is the same approach we use in CoreUI components for calculating chart scales and determining data ranges in our analytics components. For arrays with non-numeric values, filter first: Math.max(...array.filter(Number)). For very large arrays (>100k elements), consider using a traditional loop to avoid stack overflow issues. For objects, use: Math.max(...array.map(item => item.value)).


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author