# Javascript: 3 Ways to Find Min/Max Number in an Array

Last updated on June 23, 2022 A Goodman Loading...

This article walks you through a couple of different ways to find the minimum and maximum numbers in a Javascript array. Knowing more than one solution to solve a problem can help you understand Javascript more deeply and have more options to choose from in various real-world situations you may confront in the future.

Table of Contents

## Using Math.min() and Math.max() functions

Using the Math.min() and Math.max() functions with the spread operator is the shortest and most straightforward approach.

Example:

const arr = [1, 9, 8, 7, 11, -5, -8, 9.3, 12.1, 5];
console.log(Math.min(...arr));
console.log(Math.max(...arr));

Output:

-8
12.1

Note that this works with modern Javascript only (ES6 and newer).

## Using Array.reduce() method

Example:

const arr = [09, 8, 5.5, 4.9, 10, 11, 7];
const minNumber = arr.reduce((min, curr) => {
return curr < min ? curr : min;
});

console.log('min:', minNumber);

const maxNumber = arr.reduce((max, curr) => {
return curr > max ? curr : max;
});

console.log('max:', maxNumber);

Output:

min: 4.9
max: 11

## Using a For loop (the classic way)

This is the traditional solution to get the job done. It always works well but requires more code than the preceding methods.

Example code:

const arr = [20, 30, 100, 40, 200, 99];

// Find the min
var min = arr[0];
for(i = 1; i < arr.length; i++){
if(min > arr[i]){
min = arr[i];
}
}
console.log('min:', min);

// Find the max
var max = arr[0];
for(i = 1; i < arr.length; i++){
if(max < arr[i]){
max = arr[i];
}
}

console.log('max:', max);

Output:

min: 20
max: 200

## Epilogue

We’ve gone over several practical examples of seeking the minimum and maximum values in a given array with numeric elements. This essential knowledge can help you get through complex problems when programming. If you would like to explore more new and exciting stuff about Javascript and modern web technologies, take a look at the following articles:

You can also check out our Javascript category page, TypeScript category page, Node.js category page, and React category page for the latest tutorials and examples.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments