3 Easy Ways To Find largest number in array using JavaScript

We are going to learn different methods to find largest number in array using Javascript.

Largest Number in JS

Find largest number using For Loop

Method 1:

We are going to write a function to find largest number in array using for loop.

let arr = [10, 20, 30, 40, 50];

The largest number of the above array is 50.

Let’s write the program to find largest number.

function findLargestNumber(arr) {
  //initialize variable with 0
  let largest = 0;

  for (i = 0; i <= largest; i++) {
    if (arr[i] > largest) {
      largest = arr[i];
    }
  }
  return largest;
}

Explanation:

  • Explanation of the above program, we initialize variable largest with value 0.
  • We are creating for loop to find largest number in array. On the first iteration of the loop at If condition arr[0] is 10, which is greater than the largest variable we initialized the largest 10>0. The condition is true so now the largest variable is assigned with value 10.
  • This for loop will execute 50 times because the loop is based on the largest number. For example [10,20,30,1000,50], now the highest value is 1000 at this time loop will execute for 1000 times. We can see how to solve that in the next program. Now execute our function to get output.
let largestNumber = findLargestNumber(arr);

console.log(largestNumber);

You can check the above function in your browser console or checkout my CodePen.

Method 2: We are going to use the same array values used in method 1.

let arr = [10, 20, 30, 40, 50];
function findLargestNumber(arr) {
  let largest = arr[0];
  for (var i = 1; i < arr.length; i++) {
    if (largest < arr[i]) {
      largest = arr[i];
    }
  }
  return largest;
}

Explanation:

  • Explanation of the above program, we initialize variable largest with value arr[0]. Here arr[0] is 10. so var largest=10.
  • We are creating for loop to find largest number in array. Now largest is 10 & arr[0] is also 10. On checking condition 10<10 is false.
  • On second iteration largest is 10 and arr[1] is 20. 10<20 condition is true now variable largest is assigned with value 20.
  • This for loop is based on array length. If array contains 10 values it will iterate only 10 times and we will get the largest number.
  • For example [10,20,30,1000,50], now the highest value is 1000 at this time loop will not execute for 1000 times. It will execute only 5 times.
let largestNumber = findLargestNumber(arr);

console.log(largestNumber);

You can check the above function in your browser console or checkout my CodePen.

Find largest number in array using JavaScript Math.Max Function

In this method, we are going to use Math.max(). Math.max() function get an arguments as series of number input and return the largest number.

Example: Math.max(10,20,30,40,50) this will give result 50.

If we pass Array to Math.max() it will return Nan.

Example: Math.max([10,20,30,40,50]) this will give result as Nan.

let arr = [10, 20, 30, 40, 50];

Using spread operator we are going to spread out the array as series of elements then pass it to Math.max() function. Read more about Math.max() here. Read about spread operator here.

let largestNumber = Math.max(...arr);

console.log(largestNumber);

You can check the above function in your browser console or checkout my CodePen.

Find largest number in array using lodash

Lodash is a javascript library that provides utility functions for common programming tasks. we cannot test lodash program in usual browser console. Go to the Lodash’s Official website and open console then execute lodash program.

Lodash

let arr = [10, 20, 30, 40, 50];
let largestNumber = _.max(arr);

console.log(largestNumber);

It will provide result as 50.

Try to learn lodash it makes your javascript life easier.

You can check the above function in your lodash website browser console or checkout my CodePen.

Conclusion: We hope you learned few new things to find largest number in array.

In this tutorial we have found 3 ways to find a largest number in the javascript array. Which means there are many ways to solve problem for that we have to keep learn and practice javascript.

If you know any other ways to find the largest number you can put it in comment.

If you want us to explain other javascript problems put in the comment we will try to explain at our best.

Recent Blog:

What is React native?