JavaScript Find Max and Min Value from an Array

To find/get max and min value or element in an array; Through this tutorial, i am going to show you how to get or find the minimum or lowest and maximum or largest value or element from an array in javaScript.

Using min() and max() methods of javaScript; you can find the minimum value/element from the an array and find the maximum value/element from an array in javaScript. And as well as you can find min and max in javascript array using for loop.

Find the min/max element of an Array using JavaScript

  • Min() method javaScript – To find the minimum element in an array
  • Max() method javaScript – To find the maximum element in an array

Min() method javaScript

JavaScript max () method is used to find the lowest or minimum value/element from given array.

Syntax of min method

The following syntax of min method is:

Math.min(n1, n2, n3, …, nX)

Example 1 – To find the minimum value in an array in javascript

Here, i will take example using min() method to find the minimum or smallest value/element in javaScript arra; As shown below:

<script>
var numbers = [25, 45, 15, -5, 73, 6];

//find the min value from array javascript

var minValue = Math.min(...numbers);

document.write(minValue); // output:-5

</script>

Max() method javascript

JavaScript max () method is used to get or find the highest or maximum value/element from given array.

Syntax of max method

The following syntax of max method is:

 Math.max(n1, n2, n3, ..., nX)

Example 1 – To find the maximum value in an array in javascript

Here, i will take second example using max() method to find the minimum or lowest and maximum or largest value from array in javascript; As shown below:

<script>
var numbers = [25, 45, 15, -5, 73, 6];

//find the max value from array javascript

var maxValue = Math.max(...numbers);

document.write(maxValue + '<br>'); // output:73
 
</script>

Find min and max in array javascript using for loop

Using for loop with array in javaScript, you can easily find min and max value/element in array; See the following example for that:

Example 1 – To find min and max in array javascript using for loop

See the following example using for loop with array in javaScript to find min and max value/element in array; As shown below:

let arrayList = [5, 2, 3, 4, 3, 21];

//for max element from array
let max = arrayList[0];
for (let i = 1; i < arrayList.length; ++i) {
  if (arrayList[i] > max) {
    max = arrayList[i];
  }
}

// for min element from array
let min = arrayList[0];
for (let i = 1; i < arrayList.length; ++i) {
  if (arrayList[i] < min) {
    min = arrayList[i];
  }
}

console.log('Maximum value from array :- ' + max);
console.log('Minimum value from array :- ' + min);

Output of the above code; as shown below:

> Maximum value from array :- 21
> Minimum value from array :- 2

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Be First to Comment

Leave a Reply

Your email address will not be published.