var arr = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > largest ) {
largest = arr[i];
}
}
console.log(largest);
- You need to define
ior else it become a global variable. - Don't redefine largest in the loop.
- Since you're looping through the array, use
i < array.lengthinstead ofi <= largest. - Since you're comparing each of the items in the array to
largest, useif(largest < array[i])instead ofif(array > largest) - You should set largest equal to the first element in the array because what if all the numbers are negative?
arrayis a bad variable name because it's too similar toArray(the array constructor). Tryarrinstead.
One liner:
var largest = Math.max.apply(0, array);
More info here: Javascript max() function for 3 numbers
Answer from Larry Battle on Stack OverflowMDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Math โบ max
Math.max() - JavaScript | MDN
The Math.max() static method returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters. console.log(Math.max(1, 3, 2)); // Expected output: 3 console.log(Math.max(-1, -3, -2)); // Expected output: -1 const array = [1, 3, 2]; console.log(Math.max(...
Top answer 1 of 6
41
var arr = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > largest ) {
largest = arr[i];
}
}
console.log(largest);
- You need to define
ior else it become a global variable. - Don't redefine largest in the loop.
- Since you're looping through the array, use
i < array.lengthinstead ofi <= largest. - Since you're comparing each of the items in the array to
largest, useif(largest < array[i])instead ofif(array > largest) - You should set largest equal to the first element in the array because what if all the numbers are negative?
arrayis a bad variable name because it's too similar toArray(the array constructor). Tryarrinstead.
One liner:
var largest = Math.max.apply(0, array);
More info here: Javascript max() function for 3 numbers
2 of 6
27
var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
for (let i=0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
Videos
07:06
Find the largest number in an array JavaScript Tutorial - YouTube
How to Get the Largest Number in an Array | Find the Largest Number ...
03:56
JavaScript tips โ Find the maximum value in an array of numbers ...
03:39
Find max element in array JavaScript | Find largest number in array ...
Javascript: Interview Question find largest number - YouTube
05:59
Find min and max number in an Array | For Beginners | Without ...
W3Schools
w3schools.com โบ jsref โบ jsref_max.asp
JavaScript Math max() Method
The Math.max() method returns the number with the highest value. ... Math.max() is an ECMAScript1 (JavaScript 1997) feature. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
Top answer 1 of 15
12
Some ES6 magic for you, using the spread syntax:
function biggestNumberInArray(arr) {
const max = Math.max(...arr);
return max;
}
Actually, a few people have answered this question in a more detailed fashion than I do, but I would like you to read this if you are curious about the performance between the various ways of getting the largest number in an array.
2 of 15
5
There are multiple ways.
- Using Math max function
let array = [-1, 10, 30, 45, 5, 6, 89, 17];
console.log(Math.max(...array))
- Using reduce
let array = [-1, 10, 30, 45, 5, 6, 89, 17];
console.log(array.reduce((element,max) => element > max ? element : max, 0));
- Implement our own function
let array = [-1, 10, 30, 45, 5, 6, 89, 17];
function getMaxOutOfAnArray(array) {
let maxNumber = -Infinity;
array.forEach(number => { maxNumber = number > maxNumber ? number : maxNumber; })
console.log(maxNumber);
}
getMaxOutOfAnArray(array);
GeeksforGeeks
geeksforgeeks.org โบ javascript โบ javascript-max-element-in-an-array
JavaScript - Max Element in an Array - GeeksforGeeks
July 23, 2025 - The forEach method iterates over each element in the array, comparing each element to a variable (max) initialized to the first element. If the current element is greater than max, max is updated. After the loop, max holds the largest value.
GeeksforGeeks
geeksforgeeks.org โบ javascript โบ javascript-program-to-find-second-largest-element-in-an-array
JavaScript - Find Second Largest Element in an Array - GeeksforGeeks
July 23, 2025 - first is initialized to -Infinity to track the largest number, and second is initialized to -Infinity for the second largest. As we iterate through the array, we update first and second based on comparisons.
OneCompiler
onecompiler.com โบ javascript โบ 3xdvdamus
find largest number in array javascript using for loop - JavaScript - OneCompiler
let mobiles = ["iPhone", "Samsung", "Pixel"]; // accessing an array console.log(mobiles[0]); // changing an array element mobiles[3] = "Nokia"; Arrow Functions helps developers to write code in concise way, itโs introduced in ES6. Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well. ... const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0) .map(ele => ele ** 2); console.log(squaresOfEvenNumbers);
Practical Common Lisp
gigamonkeys.com โบ ai โบ index-of-largest
Index of largest
This expression uses the reduce() function on the array arr to iterate through its elements and compare their values. The function keeps track of the maximum value's index, updating it whenever it encounters a larger value. Finally, it returns the index of the largest value in the array.