JavaScript max of array | Simple example code

JavaScript Math class offers max() functions which return the largest the given numbers respectively. We can use them to find the maximum in an JS array:

You have to use other method with max() to get max value element from array:-

  • reduce()
  • apply()
  • Spread Operator

JavaScript max of array Code

HTML Example code:

Use reduce()

The recommended approach is to use Array.reduce() to find the maximum element in an array. It do comparing each value of the array:

Use apply()

Use the Function.prototype.apply() method for finding the maximum and minimum value in a numeric array.

Spread Operator (…)

Use the spread operator (...) which offers the shorter syntax of writing the apply method discussed above.

Output: Result will be same of all above example because of same array.

JavaScript max of array

Do comment if you have any doubts and suggestions on this JS array topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.