Find the min/max element of an Array using JavaScript Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.JavaScript offers several methods to achieve this, each with its advantages.Using Math.min() and Math.max() Methods The Math object's Math.min() and Math.max() methods are static methods that return the minimum and maximum elements of a given array. The spread(...) operator could pass these functions into an array. JavaScript let Arr = [50, 60, 20, 10, 40]; let minVal = Math.min(...Arr); let maxVal = Math.max(...Arr); console.log("Min Elem is:" + minVal); console.log("Max Elem is:" + maxVal); OutputMinimum element is:10 Maximum Element is:60 Math.min(...Arr) and Math.max(...Arr) find the smallest (10) and largest (60) values in [10, 20, 30, 40, 50, 60], logging "Min Elem is: 10" and "Max Elem is: 60".Iterating through the Array Iterate through the array, initializing the minimum and maximum values to Infinity and -Infinity, respectively. JavaScript let Arr = [50, 60, 20, 10, 40]; let minVal = Infi; let maxVal = -Infi; for (let item of Arr) { // Find min val if (item < minVal) minVal = item; // Find max val if (item > maxVal) maxVal = item; console.log("Min elem is:" + minVal); console.log("Min elem is:" + maxVal); } OutputMin elem is:50 Min elem is:50 Min elem is:50 Min elem is:60 Min elem is:20 Min elem is:60 Min elem is:10 Min elem is:60 Min elem is:10 Min elem is:60 minVal and maxVal are initialized to Infinity and -Infinity.The for...of loop updates minVal and maxVal by comparing each array element.The console.log statements print the minimum and maximum values during every iteration (which should be outside the loop).Using a Custom Comparator with Array.sort() MethodThe Array.sort() method sorts the elements of an array in place and returns the sorted array.. JavaScript let Arr = [50, 60, 20, 10, 40]; Arr.sort((a, b) => a - b); let minVal = Arr[0]; Arr.sort((a, b) => b - a); let maxVal = Arr[0]; console.log("Min Elem is:" + minVal); console.log("Max Elem is:" + maxVal); OutputMinimum element is:10 Maximum Element is:60 The array is sorted in ascending order for the minimum and descending order for the maximum. The first element after each sort gives the result, but sorting modifies the array. Comment More infoAdvertise with us Next Article Find the min/max element of an Array using JavaScript sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to Add Elements to a JavaScript Array? Here are different ways to add elements to an array in JavaScript.1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array.Syntaxarray.push( element1, element2, . . ., elementN );JavaScriptconst arr = [10, 20, 30, 40]; arr.push( 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J 3 min read Like