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 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.
🌐
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.
🌐
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.
🌐
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
🌐
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.
🌐
Built In
builtin.com › articles › javascript-array-max
3 Ways to Find the Maximum Value in a JavaScript Array
February 16, 2024 - let numbers = [3, 7, 2, 8, 5]; let max = numbers.reduce((accumulator, currentValue) => { return Math.max(accumulator, currentValue); }, numbers[0]); console.log(max); // Outputs: 8 · A tutorial on how to find the maximum value of an array in JavaScript.
🌐
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);
🌐
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
🌐
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 ...
🌐
YouTube
youtube.com › watch
Find the largest number in an array JavaScript Tutorial - YouTube
In this tutorial, you’ll learn how to find the largest number in an array with JavaScript which is a common coding exercise asked at Junior Developer intervi...
Published   February 6, 2019
🌐
TutorialsPoint
tutorialspoint.com › how-to-find-the-largest-number-contained-in-a-javascript-array
How to find the largest number contained in a JavaScript array?
Step 3 ? In the last step, we will write the logic to display the largest number in the array on the user screen. Let us understand with the help of a code example ? <html> <body> <h3>Finding the largest number contained in a JavaScript array</h3> <p>The Dummy array is: [20, 22, 18, 25, 75, 62, 88]</p> <p id="result"></p> <script> let arr = [20, 22, 18, 25, 75, 62, 88]; let largest = arr.reduce(function (a, b) { return (a > b) ?
🌐
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.
🌐
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 - // number array const numberArr = [23, 122, 1, 23, 4, 56]; // get highest number const highest = Math.max(...numberArr); // get lowest number const lowest = Math.min(...numberArr); console.log("Highest Number: " + highest); // Highest Number: ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-n-largest-elements-from-array-in-javascript
How to get n largest elements from array in JavaScript ? - GeeksforGeeks
July 23, 2025 - ... let largArr = new Array(); let arr = new Array(93, 17, 56, 91, 98, 33, 9, 38, 55, 78, 29, 81, 60); function largest() { largArr[0] = 0; largArr[1] = 0; largArr[2] = 0; for (i = 0; i < arr.lengt