Mark As Completed Discussion

How To Find Minimum And Maximum Value In An Array Using JavaScript

In this challenge you are going to learn how you can get the maximum and the minimum value of an array using JavaScript. First of all we need an array.

What is an Array?

In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index as shown below.

Introduction

So below we will create an array with JavaScript:

JAVASCRIPT
1const numbers = [4, 12, 7, 2, 15]

Result:

Introduction

Are you sure you're getting this? Click the correct answer from the options.

From our array, which value will be the minimum?

Click the option that best answers the question.

  • 4
  • 7
  • 2
  • 15

Now that we have created our array, we need to create two variables for the: 1. Maximum number 2. Minimum number

Minimum Value

Let's take the minimum variable, first we need to set a value for this variable, it cannot be 0.

But why?

As in our array example, all the values are greater than zero including our actual minimum value. Therefore the minimum value of our array will not be correct, it will just print a value of zero when in fact the minimum number inside this array is actually 2!

How do we overcome this?

We set the minimum value to Infinity.

JAVASCRIPT
1let minimum = Infinity

Infinity is a value in JavaScript representing mathematical infinity (∞). It is a number greater than any assignable quantity or countable number. In our case we are using it for comparison since if you compare any value to ∞, the ∞ will always be higher than that number.

Maximum Value

In the case of the maximum value, we will set it to minus Infinity (-∞) because no number is less than -∞.

JAVASCRIPT
1let maximum = -Infinity

Try this exercise. Is this statement true or false?

Infinity is greater than 5,000,000

Press true if you believe the statement is correct, or false otherwise.

Using a JavaScript Loop

Now for the comparison part, we will now compare the values inside our array to find the maximum and minimum values of our array using a For Loop

JAVASCRIPT
1for(let number of numbers){
2    if(number > maximum)
3        maximum = number
4        
5    if(number < minimum)
6        minimum = number
7 }
8 
9console.log(
10  `Maximum Value: ${getMaxOfArray(numbers)}` + `\nMinimum Value: ${getMinOfArray(numbers)}`
11)

The loop statement will loop through our array of numbers, number is the value that is being processed each time starting from the first number in our array which is 4 then 12 ... 7 ... until it reaches the final number of the array, 15.

For Loop: Maximum Value

If the number that is being processed is higher (>) than the maximum value that we have currently then the number being processed will become the new maximum value.

Initially since the maximum value is -∞, in the first loop the first number of our array will become the maximum number since all numbers are greater than -∞ (the current maximum value). In our case it will become 4.

The second loop will then run where the second number in our array (12) will be compared to our current maximum value (4). Since 12 > 4 the maximum value will now become 12 and the third loop will begin. The For Loop will continue until the last number in our array has been processed.

Using Loops

For Loop: Minimum Value

The exact same mechanism will happen for the minimum value except if the number that is being processed is less (<) than the minimum value that we have currently then the number being processed will become the new minimum value.

Using Loops

Console output

To finish the console will output the maximum and the minimum:

Using Loops
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

JavaScript's Built-in Function

With coding there is often many solutions to a problem. Didn't like the last solution? Well here's another using Javascript's built-in functions Math.min and Math.max:

JAVASCRIPT
1const numbers = [4, 12, 7, 2, 15];
2
3function getMaxOfArray(numArray) {
4  return Math.max.apply(null, numArray);
5}
6
7function getMinOfArray(numArray) {
8  return Math.min.apply(null, numArray);
9}
10
11console.log( `Maximum Value: ${getMaxOfArray(numbers)}` + `\nMinimum Value: ${getMinOfArray(numbers)}`);

What are Functions?

Functions are blocks of code designed to perform a particular task. Once we have defined a function it can be used many times.

JAVASCRIPT
1function name(parameter) {
2  // code to be executed
3};

In this case we create two functions with the names getMaxOfArray and getMinOfArray with the parameter numArray.

Console output

When we call the functions we created, and pass out numbers array through notice that we get the exact same output as before:

JavaScript's Built-in Function

Let's test your knowledge. Click the correct answer from the options.

From the JavaScript below, what will the console output be?

JAVASCRIPT
1const numbers = [4, 60, 53, 26, 2]
2
3let maximum = -Infinity
4let minimum = Infinity
5
6for(let number of numbers){
7    if(number > maximum)
8        maximum = number
9        
10    if(number < minimum)
11        minimum = number
12 }
13 
14console.log(`Maximum Value: ${getMaxOfArray(numbers)}` + `\nMinimum Value: ${getMinOfArray(numbers)}`);

Click the option that best answers the question.

  • Maximum Value: 60, Minimum Value: 2
  • Maximum Value: 53, Minimum Value: 4
  • Maximum Value: 26, Minimum Value: ∞
  • Maximum Value: -∞, Minimum Value: 2

Are you sure you're getting this? Click the correct answer from the options.

From the JavaScript below, what will the console output be?

JAVASCRIPT
1const numbers = [6, 20, 63, 2, 24]
2
3let minimum = Infinity
4let maximum = -Infinity
5
6for(let number of numbers){
7    if(number < minimum)
8        minimum = number
9        
10    if(number > maximum)
11        maximum = number
12 }
13 
14 console.log(`Minimum Value: ${getMinOfArray(numbers)}` + `\nMaximum Value: ${getMaxOfArray(numbers)}`);

Click the option that best answers the question.

  • Maximum Value: 63, Minimum Value: 2
  • Maximum Value: 24, Minimum Value: 20
  • Maximum Value: 2, Minimum Value: -∞
  • Maximum Value: 2, Minimum Value: 63

One Pager Cheat Sheet

  • You can find the maximum and minimum value of an array using JavaScript by creating it and accessing its elements.
  • The index of the array determines its minimum value, which in this case is 2, the smallest number of the entire array.
  • We set the minimum variable to Infinity and the maximum variable to -Infinity to compare values correctly.
  • Yes, Infinity is greater than 5,000,000 because it is an unbounded number that is higher than any assignable quantity.
  • Using a For Loop, a comparison of the values inside the array is conducted to find the maximum and minimum values and output them to the console.
  • Utilizing Javascript's built-in functions, Math.min and Math.max, we can create our own functions to find the minimum and maximum of an array.
  • The for loop iterates through the array and stores the largest and smallest numbers in the maximum and minimum variables which are outputted as "Max Value: 60" and "Min Value: 2".
  • The code uses a for loop to compare each number in the array and updates the values of minimum and maximum according to the result, then uses getMaxOfArray and getMinOfArray functions to return the maximum and minimum values.