Finding largest number in array JavaScript
In this post, you will learn how to find the largest number in an array using the JavaScript.
An array is a collection of key-value pairs. We can store more than one item in a single variable, i.e., an array. So, an array is used when there is a requirement to add more items in a single variable. A JavaScript array is treated like a global object that is used to create an array. Here, we have used different ways to find the largest number in an array using the JavaScript.
Using the Math.max() method
The Math.max() method returns the number with the highest value. We can get the max value in the array, using this method. We can get the index of max value by calling the indexOf() method on the array, passing it the max value.
const arr = [14, 2, 70, 12, 29];
const max = Math.max(...arr);
const index = arr.indexOf(max);
console.log(index);
Output of the above code:
2
Using the forEach() array method
The forEach() method of JavaScript calls a function and iterates over the elements of an array. In the given program, first we initialize a temp variable with 0 value. Then, inside the loop compare the value of temp with the current element inside the loop. If temp is smaller, then assign the current element value into temp. By this way, we can get the largest element in an array.
let arr = [53, 51, 42, 94, 49, 83];
temp = 0;
arr.forEach((element) => {
if (temp < element) {
temp = element;
}
});
console.log(`Largest element in given array: ${temp}`);
Output of the above code:
Largest element in given array: 94
Using the reduce() array method
The reduce() method executes a reducer function on each element of the array and returns a single output value.
The value you return from this function will be the accumulatedValue used for the next iteration of the reduce() method. For the first iteration, the accumulatedValue will be assigned with the value at index zero. When the iteration is finished, the reduce() method will return a single value, which is the accumulatedValue.
let arr = [82, 54, 23, 90, 55, 29, 19];
let largestNum = arr.reduce(function (accumulatedValue, currentValue) {
return Math.max(accumulatedValue, currentValue);
});
console.log(`The largest number: ${largestNum}`);
Output of the above code:
The largest number: 90
Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh