How to Find Max/Min Value in JavaScript Array

Ammar Ali Feb 02, 2024
  1. Find the Minimum Value of an Array Using the Math.min() Function in JavaScript
  2. Find the Maximum Value of an Array Using the Math.max() Function in JavaScript
How to Find Max/Min Value in JavaScript Array

This tutorial will discuss how to find the minimum and maximum value of an array using the Math.min() and Math.max() function in JavaScript.

Find the Minimum Value of an Array Using the Math.min() Function in JavaScript

To find the minimum value present in a given array, we can use the Math.min() function in JavaScript. This function returns the minimum value present in a given array. For example, let’s define an array with some random values and find its minimum value using the Math.min() function and show it on the console using the console.log() function. See the code below.

var myArray = [1, 5, 6, 2, 3];
var m = Math.min(...myArray);
console.log(m)

Output:

1

As you can see in the output, the minimum value of the array is returned by the Math.min() function. Some browsers may not support the above method, so that you can use the apply() function along with the Math.min() function to get the minimum value from a given array. For example, see the below code.

var myArray = [1, 5, 6, 2, 3];
var m = Math.min.apply(null, myArray);
console.log(m)

Output:

1

The apply() function calls a function with a given this value and a given array in the above code. If you don’t want to use any predefined function, you can make your own function using a loop in JavaScript. For example, let’s create a function to find the minimum value of an array. See the code below.

function MyMin(myarr) {
  var al = myarr.length;
  minimum = myarr[al - 1];
  while (al--) {
    if (myarr[al] < minimum) {
      minimum = myarr[al]
    }
  }
  return minimum;
};
var myArray = [1, 5, 6, 2, 3];
var m = MyMin(myArray);
console.log(m)

Output:

1

In the above code, we saved the last element of the given array to the variable minimum, and we compared it with the previous element. If the element is less than the variable minimum, we will store this element in the variable minimum. And if not, we will move to the next element. We will repeat this procedure until we reached index 0. After the loop, we will return the variable minimum.

Find the Maximum Value of an Array Using the Math.max() Function in JavaScript

To find the maximum value present in a given array, we can use the Math.max() function in JavaScript. This function returns the maximum value present in a given array. See the code below.

var myArray = [1, 5, 6, 2, 3];
var m = Math.max(...myArray);
console.log(m)

Output:

6

You can also use the apply() function along with the Math.max() function to get the maximum value from a given array. For example, see the below code.

var myArray = [1, 5, 6, 2, 3];
var m = Math.max.apply(null, myArray);
console.log(m)

Output:

6

Let’s create a function to find the maximum value of an array. See the code below.

function MyMax(myarr) {
  var al = myarr.length;
  maximum = myarr[al - 1];
  while (al--) {
    if (myarr[al] > maximum) {
      maximum = myarr[al]
    }
  }
  return maximum;
};
var myArray = [1, 5, 6, 2, 3];
var m = MyMax(myArray);
console.log(m)

Output:

6
Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - JavaScript Array