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 i or else it become a global variable.
  • Don't redefine largest in the loop.
  • Since you're looping through the array, use i < array.length instead of i <= largest.
  • Since you're comparing each of the items in the array to largest, use if(largest < array[i]) instead of if(array > largest)
  • You should set largest equal to the first element in the array because what if all the numbers are negative?
  • array is a bad variable name because it's too similar to Array (the array constructor). Try arr instead.

One liner:

var largest = Math.max.apply(0, array);

More info here: Javascript max() function for 3 numbers

Answer from Larry Battle on Stack Overflow
๐ŸŒ
MDN 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(...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-find-largest-element-in-an-array
JavaScript Program to Find Largest Element in an Array - GeeksforGeeks
July 23, 2025 - In this approach, Using a for loop, iterate through the array, comparing each element to a running largest value, updating it if a larger element is encountered, and returning the final largest value.
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-find-the-maximum-value-in-an-array-in-javascript
How to find the maximum value in an array in JavaScript ยท CoreUI
September 21, 2025 - This approach is concise, readable, and leverages JavaScriptโ€™s built-in mathematical functions for optimal performance. Use Math.max() with the spread operator to find the largest value in an array.
๐ŸŒ
Medium
medium.com โ€บ @amitsharma_24072 โ€บ 3-efficient-ways-to-find-maximum-value-in-javascript-arrays-515463ebd17
3 Efficient Ways to Find Maximum Value in JavaScript Arrays | by Amit Sharma | Medium
July 11, 2024 - The Math.max() function is a built-in JavaScript function that takes any number of arguments and returns the maximum value. By using the spread operator (โ€ฆ) and applying it to the array: const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]; const max ...
๐ŸŒ
danvega.dev
danvega.dev โ€บ blog โ€บ find-max-array-objects-javascript
How to find the max id in an array of objects in JavaScript
March 14, 2019 - While I like the map and reduce approach much better than iterating over the array it still doesn't feel great to me. If you dig into Math.max you will find out that it can take a list of numbers or an array of numbers.
๐ŸŒ
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: ...
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ domhabersack โ€บ getting-the-largest-number-from-an-array-e97
๐Ÿ”ฅ Getting the largest number from an array - DEV Community
January 5, 2021 - Math.max() returns the largest of zero or more numbers passed to it. We can use the spread operator w... Tagged with javascript.
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ javascript-array-max
3 Methods to Find the JavaScript Array Max | Built In
JavaScriptโ€™s Math.max() can find the maximum among individual numbers. However, when combined with the spread operator, it can find the maximum in an array, making this method both concise and efficient.
๐ŸŒ
Medium
medium.com โ€บ free-code-camp โ€บ three-ways-to-return-largest-numbers-in-arrays-in-javascript-5d977baa80a1
Three ways you can find the largest number in an array using JavaScript | by Sonya Moisset | Weโ€™ve moved to freeCodeCamp.org/news | Medium
February 16, 2017 - Such functions are known as variadic functions, and they can accept any number of arguments instead of a fixed one. The Math.max() function returns the largest of zero or more numbers, and we can pass any number of arguments.
๐ŸŒ
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);
๐ŸŒ
DEV Community
dev.to โ€บ melvin2016 โ€บ how-to-get-the-highest-and-lowest-number-from-an-array-in-javascript-21ml
How to get the highest and lowest number from an array in JavaScript? - DEV Community
November 29, 2020 - To get the highest or lowest number from an array in JavaScript, you can use the Math.max() or the Math.min() methods then spread the elements from the array to these methods using the spread operator (...).
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ largest-three-numbers-in-array-5214f68cf679
How to Find the Largest Three Numbers in an Array (JavaScript) | by Cosmocoder | JavaScript in Plain English
August 26, 2022 - So here Iโ€™ve set up the basic framework. Iโ€™ve defined the three-pointers to store the three highest values, set up the loop to iterate over the input and then Iโ€™m returning the sorted array of the highest values.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ three-ways-to-return-largest-numbers-in-arrays-in-javascript-5d977baa80a1
Three ways you can find the largest number in an array using JavaScript
October 17, 2016 - Such functions are known as variadic functions, and they can accept any number of arguments instead of a fixed one. The Math.max() function returns the largest of zero or more numbers, and we can pass any number of arguments.
๐ŸŒ
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.