You are currently viewing How to Find the Largest Number in JavaScript

How to Find the Largest Number in JavaScript

Largest Number in JavaScript

In this article, we will see how to find the largest number in javascript with example codes. If you have two are more numbers in the array, you can use Math.max.apply() method.

Mehtod 1

let arr = [44, 28, 18, 55, 68, 11, 70];
let largest = Math.max.apply(null, arr);
  1. See the above code, we have created an array and inside it, we gave some random numbers.
  2. To find the largest number, we need to use Math.max.apply() method and pass the value “null” and your array name as in the above code.
  3. See the output the largest number is displayed successfully.

Method 2

let arr = [3, 45,89,99, 98, 95];
let max = arr[0];

for (let i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
  1. In this above code, we have created and assigned the first value of an array to the “max” variable.
  2. Then we create a loop and it runs until the array length, so we set the condition arr.length.
  3. Inside the loop, we check the condition “arr[i] > max”, if the condition is true max value is changed to the largest number.
  4. Finally print the max value and it returns the largest value of an array.

Leave a Reply