Find the min/max element of an Array using JavaScript
The minimum and maximum element in an array can be found using 2 approaches:
Method 1: Using Math.min() and Math.max()
The min() and max() methods of the Math object are static functions that return the minimum and maximum element passed to it. These functions could be passed an array with the spread(…) operator. The spread operator allows an iterable to expand in places where multiple arguments are expected. In this case, it automatically expands array and gives the numbers to the functions.
Syntax:
minValue = Math.min(...array); maxValue = Math.max(...array);
Example 1:
<!DOCTYPE html> < html > < head > < title > Find the min/max element of an Array using JavaScript </ title > </ head > < body > < h1 style = "color: green" > GeeksforGeeks </ h1 > < b >Find the min/max element of an Array using JavaScript</ b > < p >Click on the button below t o find out the minimum and maximum of the array [50, 60, 20, 10, 40]</ p > < p >Minimum element is: < span class = "min" > </ span > < br >Maximum Element is: < span class = "max" > </ span ></ p > < button onclick = "findMinMax()" > Click to check </ button > < script > function findMinMax() { array = [50, 60, 20, 10, 40]; minValue = Math.min(...array); maxValue = Math.max(...array); document.querySelector( '.min').textContent = minValue; document.querySelector( '.max').textContent = maxValue; } </ script > </ body > </ html > |
Output:
- Before clicking the button:
- After clicking the button:
Method 2: Iterating through the array and keeping track of the minimum and maximum element
The minimum and maximum element can be kept track by iterating through all the elements in the array and updating the minimum and maximum element upto that point by comparing them to the current minimum and maximum values. Initially, the minimum and maximum values are initialized to Infinity and -Infinity.
Syntax:
minValue = Infinity; maxValue = -Infinity; for (item of array) { // find minimum value if (item < minValue) minValue = item; // find maximum value if (item > maxValue) maxValue = item; }
Example:
<!DOCTYPE html> < html > < head > < title > Find the min/max element of an Array using JavaScript </ title > </ head > < body > < h1 style = "color: green" > GeeksforGeeks </ h1 > < b > Find the min/max element of an Array using JavaScript </ b > < p > Click on the button below to find out the minimum and maximum of the array [50, 60, 20, 10, 40] </ p > < p >Minimum element is: < span class = "min" > </ span > < br >Maximum Element is: < span class = "max" > </ span ></ p > < button onclick = "findMinMax()" > Click to check </ button > < script > function findMinMax() { array = [50, 60, 20, 10, 40]; minValue = Infinity; maxValue = -Infinity; for (item of array) { // find minimum value if (item < minValue ) minValue = item ; // find maximum value if (item > maxValue) maxValue = item; } document.querySelector( '.min').textContent = minValue; document.querySelector( '.max').textContent = maxValue; } </ script > </ body > </ html > |
Output:
- Before clicking the button:
- After clicking the button:
Recommended Posts:
- How to append an element in an array in JavaScript?
- How to remove an element from an array in JavaScript?
- How to select a random element from array in JavaScript ?
- JavaScript | Array find() function
- JavaScript | Array.find() Method
- Find the index of an array element in Java
- PHP program to find missing element(s) from an array
- How to find the index of all occurrence of elements in an array using JavaScript ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to add a class to DOM element in JavaScript?
- How to set the value of a select box element using JavaScript?
- How to get the type of DOM element using JavaScript?
- JavaScript | How to add an element to a JSON object?
- Print the content of a div element using JavaScript
- How to get the data attributes of an element using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.