JavaScript Magic: One Line to Find the Maximum Value in an Array

JavaScript Magic: One Line to Find the Maximum Value in an Array
Photo by Greg Rakozy / Unsplash

Finding the maximum value in an array is a common task in various programming scenarios. While you could write a for loop to iterate through the array, JavaScript's Math.max() method, combined with the spread syntax, offers a concise and elegant solution.

const scores = [85, 92, 78, 95, 88];
const highestScore = Math.max(...scores);
console.log(highestScore); // Output: 95

Happy coding!