1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Array Maximum and Minimum Values

Join the Discussion

Questions? Comments?

One of the advantages to JavaScript is that if the language doesn't provide the exact code that we want to use, we can usually add the functionality ourselves. For example, let's say that we need to be able to add the ability to retrieve the maximum and/or minimum value that we have stored in an array.

Where we only need to do that from one place in our code we could set up a loop to go through the array to find the largest or smallest value. The code would need to be commented though since it will not necessarily be obvious at a later date or to someone else that the statements in the loop are supposed to be locating the maximum or minimum value in the array. Where we need to retrieve the values multiple times or from miltiple arrays, the code can become particularly messy with all the loops that we'd need to add.

JavaScript provides us with a better way to extract the maximum or minimum value from an array, we can add max() and min() methods to the existing Array object so as to add the ability to find the maximum and minimum values from any array at any time by using a simple statement to retrieve the required value.

Here is one way that we could implement these two methods.

Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
return max;
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
return min;

Note that this code is not very efficient because it needs to searchthe entire array every time you request the value. This allows the code to work even if the contents of the array change between calls with at least the possibility that the values of the maximum and minimum entries will have changed. If we know that we will often need to reference either of those values more than once and that the array will be unchanged in the meantime, we could change the methods so that they only process through once and internall retain the value that can just be returned directly. The problem with doing this though is that the method will no longer return the correct value if the array does change between calls. A better way to handle that is to do the method call once and store the value into a variable and reference that for the subsequent times you need to access it. That makes it more obvious where you are using the same value as previously retrieved and when you want to check the array again.

Note that when you code the lookups this way the code is effectively self documenting and no comments are required (and if you don't think min() and max() methods are self documenting enough as to what they do then simply name them something that you think is more descriptive.

These two methods have the advantage that they will work regardless of the type of content within the array since the only thing we are doing within the loop is comparing two values which works regardless of the value types.

Actually using these methods is fairly simple. Supporse we look at two examples, one of an array containing numbers and the other an array containing letters. We will then call each of the methods for each array and then examine the values returned.

var ary = [5,8,2,4,11,7,3];
var ary2 = ['f','g','r','s','a','j','q'];

With these two arrays and the four alert statements calling the methods so we can see what they return we will have four alerts pop up containing 11, 2, 's', and 'a' respectively. Of course you don't need to piut the method calls in an alert statement, you would use it where ever you want to do the array lookup to find the appropriate value.

©2017 About.com. All rights reserved.