Find the largest number in array JavaScript | Example code

You can use for loop or reduce() method to get the largest number in array JavaScript. But the simplest way is Math.max() function.

3 Way to find largest number in array JS:

  • Math.max()
  • reduce() method
  • FOR loop

Find the largest number in array JavaScript Example

Let’s see one by every method in HMTL code:

Math.max()

Some ES6 magic for you, using the spread syntax.

reduce() method

FOR loop

Let’s create a function for it:-

Create an array with the name of max then initialize max as the first element, then traverse the given array from the second element till the end. For every traversed element, compare it with max, if it is greater than max, then update max.

Output: Largest number will be same for all example because of Array values are same.

Find the largest number in array JavaScript

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.