In this tutorial, you’ll be going to learn how to find max and min value from an array in javascript. The easiest approach to use javascript math functions which is `Math.max()`

and `Math.min()`

a function provided by javascript.

You’ll also be going to find max and min value from an array in javascript without using javascript function. Because when you are facing an interview, sometimes an interview wants you to implement the same thing without using a function to check your logics.

**JavaScript Array** is a collection of different types of value but in our case, we are using an array which is a collection of number.

So we are showing multiple ways to find the maximum value in an array and to find the smallest value in an array. So stick to the end of the post because the answer to one interview question can impress interviewers.

#### You can also check :

- Phone Number Validation in HTML5
- Convert PHP array to JavaScript array
- How To Get Multiple Elements By Id Using Javascript
- How to Reverse a String in JavaScript

## Find Max and Min Value from an Array in JavaScript

- Table Of Content
- Find Max Value In Array
- Get the Smallest Value In Javascript Array
- Use
`Math.max()`

and`Math.min()`

Without Apply Using Spread Operator - Using ES5
`Array.forEach()`

To Iterate and Array and Get Max and Min Value - Using ES5
`Array.reduce()`

To Get Max and Min Number in JavaScript Array - Find Max And Min Number In Array Without Using Function

You’ll be going to learn all the above point as we are going to discuss each point in detail. So stay calm and grab carefully.

## Find Max Value In Array

So we have javascript built-in math max function which can easily find the maximum value from an array. So let’s move to our code section.

```
// javascript math max function
let max_value = Math.max(1,2,3,4,5)
console.log(max_value)
// result => 5
```

As `Math.max()`

the function does not work with an array of number but there is some way to do this.

`Function.prototype.apply()`

allows you to call a function with a given `this`

value and an *array* of arguments. So let’s implement it.

```
// crate a javscript array
let numbers_array = [1,2,3,4,5]
// javascript math max function with apply
let max_value = Math.max.apply(null, numbers_array)
console.log(max_value)
// result => 5
```

So we get the max value from an array using javascript `Math.max()`

function.

## Get the Smallest Value In Javascript Array

Now you’ll need to find the minimum value from an array. So we are going to use javascript function `Math.min()`

.

```
// javascript math min function
let min_value = Math.min(1,2,3,4,5)
console.log(min_value)
// result => 1
```

As we have the same problem with `Math.max()`

, to use array we need to use `Function.prototype.apply()`

.

```
// crate a javscript array
let numbers_array = [1,2,3,4,5]
// javascript math min function with apply
let min_value = Math.min.apply(null, numbers_array)
console.log(min_value)
// result => 1
```

So, we get the smallest number in the array and it just took one line of code and javascript min function.

## Use Math.max() and Math.min() Without Apply Using Spread Operator

As it is not necessary to use `Function.prototype.apply()`

we can also use `...`

spread operator. Let’s look at an example.

```
// crate a javscript array
let numbers_array = [1,2,3,4,5]
// javascript math max function with spread operator
let max_value = Math.max(...numbers_array)
let min_value = Math.min(...numbers_array)
console.log(max_value)
// result => 5
console.log(min_value)
// result => 1
```

So `...spread`

operator causes the array to expand in a function argument.

## Using ES5 `Array.forEach()`

To Iterate an Array and Get Max and Min Value

In ES5, we have `Array.forEach()`

to loop through an array in javascript and get track of highest and smallest value in an array. Now let’s take an example.

```
let numbers_array = [1,2,3,4,5]
let max_value = -Infinity
let min_value = Infinity
numbers_array.forEach(element => {
// find minimum value
if (element < min_value)
min_value = element
// find maximum value
if (element > max_value)
max_value = element
})
console.log(max_value)
// result => 5
console.log(min_value)
// result => 1
```

As we iterate an array and track for max and min value in array using two variables. It also might happen that array contains duplicates in an array-like `[1,1,2,3,4,5,5]`

but still, our result will be the same.

you can also check: how to remove duplicates from array in javascript

## Using ES5 `Array.reduce()`

To Get Max and Min Number in JavaScript Array

As `Array.reduce()`

, has a nice argument in a callback function which acts as an accumulator. Using this argument we find the max and min number in an array.

Now move to an example.

```
let numbers_array = [1,2,3,4,5]
let [max_value, min_value] = numbers_array.reduce((max_min,element) => {
// find minimum value
if (element < max_min[1])
max_min[1] = element
// find maximum value
if (element > max_min[0])
max_min[0] = element
return max_min;
},[-Infinity, Infinity])
console.log(max_value)
// result => 5
console.log(min_value)
// result => 1
```

So we pass a default value, which use to compare through an array for max number in the array and min number in the array. So max_min array contains both values.

## Find Max And Min Number In Array Without Using Function

Most of the time it happens when interviewer ask a developer that, tries to implement the same thing without using any inbuilt function because it might happen that the function like `Math.max()`

and `Math.min()`

not available on another technology stack.

Because what matters is a logic to implement. If you know the logic then language just acts a medium.

Now move on to our code section.

```
let numbers_array = [1,2,3,4,5]
let max_value = -Infinity
let min_value = Infinity
for(let i = 0; i < numbers_array.length; i++) {
let element = numbers_array[i]
// find minimum value
if (element < min_value)
min_value = element
// find maximum value
if (element > max_value)
max_value = element
}
console.log(max_value)
// result => 5
console.log(min_value)
// result => 1
```

So using `for loop`

we have to find the max number in an array which is 5 and min value in an array is 1. As you can use `for loop`

in any language.

You can also use a `while loop`

to get the same result.

## Conclusion:

As you can see there are many ways you can implement for *how to find min and max value in javascript array*

We have used built-in math function provided by JavaScript like** **`Math.max()`

and `Math.min()`

.

And we have also used the ES5 method like `Array.forEach()`

and `Array.reduce()`

to get the same result.

We also find max and min number in an array without using javascript function.

I hope you read the full article and I am very excited to get a comment on this post to get your review.

If you have any question please feel free to ask me in the comment section and also let me know if you have any suggestion is also acceptable. Also, like our Facebook Page.