# Find the min/max element of an Array using JavaScript

• Difficulty Level : Basic
• Last Updated : 13 Dec, 2022

The minimum and maximum elements 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 into 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 the array and gives the numbers to the functions.

Syntax:

```minValue = Math.min(...array);
maxValue = Math.max(...array);```

Example: In this example, we will see the basic implementation of Javascript Math.min() and Math.max() functions.

## HTML

 `<``body``>``    ``<``h1` `style``=``"color: green"``>``        ``GeeksforGeeks``    ````    ``<``b``>Find the min/max element of``        ``an Array using JavaScript`` ` `    ``<``p``>Click on the button below t``        ``o find out the minimum and``        ``maximum of the array``        ``[50, 60, 20, 10, 40]`` ` `    ``<``p``>Minimum element is: <``span` `class``=``"min"``>``        ````        ``<``br``>Maximum Element is: <``span` `class``=``"max"``>``        ````    ````    ``<``button` `onclick``=``"findMinMax()"``>``        ``Click to check``    ````    ``<``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;``        ``}``    `````

Output:

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 up to 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: In this example, we will be iterating through the elements of the array to find the max and min elements from the array.

## html

 `<``h1` `style``=``"color: green"``>``    ``GeeksforGeeks````<``b``>``    ``Find the min/max element``    ``of an Array using JavaScript````<``p``>``    ``Click on the button below to``    ``find out the minimum and``    ``maximum of the array``    ``[50, 60, 20, 10, 40]``` `<``p``>Minimum element is: <``span` `class``=``"min"``>``    ````    ``<``br``>Maximum Element is: <``span` `class``=``"max"``>``    ``````<``button` `onclick``=``"findMinMax()"``>``    ``Click to check````<``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;``    ``}```

Output:

My Personal Notes arrow_drop_up