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 Web Docs
The largest of the given numbers. Returns NaN if any of the parameters is or is converted into NaN. Returns -Infinity if no parameters are provided. Because max() is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created (Math is not a constructor). Math.max.length is 2, which weakly signals that it's designed to handle at least two parameters. ... Array.prototype.reduce() can be used to find the maximum element in a numeric array, by comparing each 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 - For arrays with non-numeric values, filter first: Math.max(...array.filter(Number)). For very large arrays (>100k elements), consider using a traditional loop to avoid stack overflow issues. For objects, use: Math.max(...array.map(item => item.value)). ... Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek on LinkedIn Follow Łukasz Holeczek on X (Twitter) Łukasz Holeczek, Founder of CoreUI, is a seasoned Fullstack Developer and entrepreneur with over 25 years of experience.
Discussions

Finding largest integer in an array in JavaScript - Stack Overflow
Possible Duplicate: How might I find the largest number contained in a JavaScript array? I am having trouble getting this code to work. I have been at it for a while trying to figure it out. When ... More on stackoverflow.com
🌐 stackoverflow.com
algorithm - How might I find the largest number contained in a JavaScript array? - Stack Overflow
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108] Is there a function that would find the largest number in this array? More on stackoverflow.com
🌐 stackoverflow.com
javascript - Finding the max value of a property in an array of objects - Stack Overflow
With a large array, Math.max will be called with a large number of arguments, which can cause stack overflow. ... Sign up to request clarification or add additional context in comments. ... Could you expand this answer to show how to return the object that the max value was found in? More on stackoverflow.com
🌐 stackoverflow.com
Find the biggest number in an array challenge
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/ I have done this challenge and came up with a way that it does return you a new array consists of all the biggest numbers from the sub-arrays. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 4, 2018
🌐
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.
🌐
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 - Note that the first argument to apply() sets the value of ‘this’, not used in this method, so you pass null. Now that you have a method to return the largest number in a array, you can loop through each sub-arrays with the map() method and ...
🌐
Built In
builtin.com › articles › javascript-array-max
3 Ways to Find the Maximum Value in a JavaScript Array
February 16, 2024 - 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.
Find elsewhere
🌐
Seanconnolly
seanconnolly.dev › javascript-find-element-with-max-value
How to get the element with the max value in a JavaScript array · Sean Connolly
Let's apply Array.reduce to our use case to find the max value element in our array. const playerWithMostWins = players.reduce( (prev, current) => { return prev.wins > current.wins ? prev : current } ); And if you need to change the logic to the player with the least wins (for the wall of shame?), it's the same function but just with a different comparison function:
🌐
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...
🌐
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 - We can use this method to sort ... => { return b - a; }); const max = arr[0]; console.log(max); // Output: 9 · The Math.max() function is a built-in JavaScript function that takes any number of arguments and returns the maximum ...
🌐
YouTube
youtube.com › watch
How to Get the Largest Number in an Array | Find the Largest Number from Array - YouTube
How to get the largest number in an array or how to find the largest number from array. How to find largest number in array in JavaScript? It is a common jav...
Published   January 26, 2023
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Find the biggest number in an array challenge - JavaScript - The freeCodeCamp Forum
September 4, 2018 - https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/ I have done this challenge and came up with a way that it does return you a new array consists of all the biggest numbers from the sub-arrays.
🌐
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 - The reduce() method applies a function against an accumulator and each value of the array to reduce it to a single value. The ternary operator is the only JavaScript operator that takes three operands. This operator is used as a shortcut for the if statement. (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber; ... if (currentLargestNumber > previousLargestNumber == true) { return currentLargestNumber; } else { return previousLargestNumber; } ... function largestOfFour(mainArray) { // Step 1.
🌐
W3Schools
w3schools.com › jsref › jsref_max.asp
JavaScript Math max() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-max-id-in-array-of-objects
Get the Max id in an Array of Objects in JavaScript | bobbyhadz
How to get the Max/Min values in an object. ... Use the Array.map() method to get an array of IDs. Pass the array of IDs as an argument to the Math.max() method. The Math.max() method returns the largest of the given numbers.
🌐
Quora
quora.com › How-do-you-write-a-program-in-JavaScript-to-find-the-largest-number-in-an-array-using-a-loop
How to write a program in JavaScript to find the largest number in an array using a loop - Quora
Answer (1 of 3): I don't really program in JS, but this is the general recipe, so you will still have some fun implenting it, 1. Easiest way, if you have a sort function available, is to sort the array first.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-max-element-in-an-array
JavaScript - Max Element in an Array - GeeksforGeeks
July 23, 2025 - The spread operator (...) allows an iterable such as an array to be expanded in places where zero or more arguments or elements are expected. This operator can be used with Math.max() to find the largest element in the array.