Copyvar 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);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

  • 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
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(...array)); // Expected output: 3
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › MAX_SAFE_INTEGER
Number.MAX_SAFE_INTEGER - JavaScript | MDN
const x = Number.MAX_SAFE_INTEGER ... true · 9007199254740991 (9,007,199,254,740,991, or ~9 quadrillion). Double precision floating point format only has 52 bits to represent the mantissa, so it can only safely represent integers between -(253 – 1) and 253 – 1....
🌐
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 › @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 - For loop will iterate over the array and compare each element to find the maximum value: const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]; let max = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } console.log(max); // Output: 9 · The sort() method is a built-in JavaScript method that sorts the elements of an array.
🌐
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 ... 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
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-max-element-in-an-array
JavaScript - Max Element in an Array - GeeksforGeeks
July 23, 2025 - ... let a = [22, 65, 1, 39]; let i; let max = a[0]; for (i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; } console.log(max); ... The JavaScript Math max() Method is used to return the largest of zero or more numbers.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › max
JavaScript Math max() - Find Maximum Value | Vultr Docs
November 27, 2024 - Note that JavaScript treats 0 and -0 as equal, and both are considered less than any positive number. Understand that direct comparisons using Math.max() are typically more performant and concise than manual iterative approaches. ... const largeNumArray = Array(1000000).fill().map((_, i) => i); console.log(Math.max(...largeNumArray)); Explain Code
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › find-the-min-max-element-of-an-array-using-javascript
Min and Max of an Array in JavaScript - GeeksforGeeks
To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.
Published   August 2, 2025
🌐
YouTube
youtube.com › code 2020
JavaScript tips — Find the maximum value in an array of numbers - YouTube
You can find the largest number in an array in #JavaScript with a single function call and without writing any loopsJust call Math.max(...yourArray) to get t
Published   October 5, 2022
Views   4K
🌐
GitHub
gist.github.com › 3876331
Javascript Max Number in an Array · GitHub
Javascript Max Number in an Array · Raw · maxNumberinArray.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
W3docs
w3docs.com › javascript
How to Find the Min/Max Elements in an Array in JavaScript
let arrayList = [1, 2, 3, 4, 3, 21, 0]; let max = arrayList[0]; for (let i = 1; i < arrayList.length; ++i) { if (arrayList[i] > max) { max = arrayList[i]; } } console.log(max); ... testArr.reduce(function (a, b) { return Math.max(a, b); }); ...
🌐
Programiz
programiz.com › javascript › library › math › max
JavaScript Math max()
// max() with a spread operator let maxNum = Math.max(...numbers); console.log(maxNum); // Output: 55 · In the above example, we have created an array named numbers.
🌐
Medium
medium.com › @vladbezden › how-to-get-min-or-max-of-an-array-in-javascript-1c264ec6e1aa
How to get min or max of an array in JavaScript | by Vlad Bezden | Medium
September 2, 2016 - That is because Math.min or Math.max functions expect distinct variables and not an array. So in order to do that before ES6/ES2015 apply method was used · var nums = [1, 2, 3] Math.min.apply(Math, nums) // 1 Math.max.apply(Math, nums) // 3 Math.min.apply(null, nums) // 1 Math.max.apply(null, nums) // 3 · With ES6/ES2016 destructuring assignment it becomes easier · The destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects into distinct variables.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › min and max value of an array
Min and max value of a JavaScript array - 30 seconds of code
December 30, 2023 - fn : val => val[fn])); minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], x => x.n); // 2 minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 2 maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], x => x.n); // 8 maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 8 ... An article collection of math helpers and algorithms implemented in JavaScript. ... JavaScript arrays have a very robust API offering some amazing tools.
🌐
GeeksforGeeks
geeksforgeeks.org › find-the-min-max-element-of-an-array-using-javascript
Find the min/max element of an Array using JavaScript | GeeksforGeeks
To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.
Published   January 7, 2025