Find Min and Max Value in Array JavaScript

Find Min and Max Value in Array JavaScript

In this tutorial, you will learn how to get or find the minimum or lowest and maximum or largest value from an array in javascript.

JavaScript Get Min and Max Value in Array

First of all, you must know about the min() and max() methods of javascript. You can use the min() method to get or find the minimum value from the js array and you can use the max() method to get or find the maximum value from an array in javascript.

Min() method javascript

JavaScript max () method, which is used to get or find the lowest or minimum value from specified values.

Syntax

The syntax of min method is:

Math.min(n1, n2, n3, …, nX)

Max() method javascript

JavaScript max () method, which is used to get or find the highest or maximum value from specified values.

Syntax

The syntax of max method is:

 Math.max(n1, n2, n3, ..., nX)

Example 1 – javascript find min and max value in array with the apply() Method

Let’s take the first example, to find the minimum and maximum value in array javascript. See the example below:

<script>
var numbers = [25, 45, 15, -5, 73, 6];

//find the max value from array javascript
var maxValue = Math.max.apply(null, numbers);

document.write(maxValue + '<br>'); // output:73
 
//find the min value from array javascript
var minValue = Math.min.apply(null, numbers);

document.write(minValue); // output:-5
</script>

Example 2 – javascript find min and max value in the array

Let’s take the second example, to find the minimum or lowest and maximum or largest value from array in javascript. See the example below:

<script>
var numbers = [25, 45, 15, -5, 73, 6];

//find the max value from array javascript
var maxValue = Math.max(...numbers);

document.write(maxValue + '<br>'); // output:73
 
//find the min value from array javascript
var minValue = Math.min(...numbers);

document.write(minValue); // output:-5
</script>

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *