# JavaScript Get Highest Value In Array

JavaScript Get Highest Value In Array | The highest of no or more numbers is returned by the Math.max() method. If no arguments are specified, the output is “-Infinity,” and if at least one of the parameters cannot be transformed to a number, the output is NaN.

Since max() is a static Math method, it is always referred to as Math.max() rather than as a way of a Math object.

Let’s have a look at some illustrations which will give you a better understanding of how to get the highest value in an array in JavaScript.

Example 1:
[1, 2, 3, 4]
Max value in the array = 4

Example 2:
[-11, -134, -9, -2]
Max value in the array = -2

We will discuss two methods by which we can get maximum value in array JavaScript.

## JavaScript Get Highest Value in Array Using Math.max()

The Math object’s min() and max() methods are static functions that return the minimal and maximum elements provided to them. The spread(…) operator can be used to pass an array to these functions. When an iterable is expanded in areas where many arguments are expected, the spread operator is used. In this situation, the array is automatically expanded and the numbers are passed to the functions.

Example 1: Program to get maximum value in array JavaScript using Math.max()

Simply comparing each value, array.reduce() can be used to discover the greatest element in a numeric array:-

``````var arr = [88, 23, 34, 19, 90];
var max = arr.reduce(function (a, b) {
return Math.max(a, b);
}, -Infinity);
console.log("The highest value in the array: ", max);``````

Output:-

The highest value in the array: 90

Example 2: JavaScript find highest value in array by Math.max() using negative input

``````var arr = [-8, -23, -34, -19, -90];
var max = arr.reduce(function (a, b) {
return Math.max(a, b);
}, -Infinity);
console.log("Highest value in array: ", max);``````

Output:-

The highest value in the array: -8

## JavaScript Find Highest value in Array by Comparision

Iterating through all of the array values and changing the highest value up to that point by matching them to the existing highest values can be done to keep track of the highest element. The highest value is initially set -Infinity,

``````var arr = [12, 23, 44, 54, 19, 78];
var max = arr;
for (var k = 1; k < arr.length; k++) {
if (arr[k] > max) {
max = arr[k];
}
}
console.log("The highest value is: " + max);``````

Output:-

The highest value is: 78

Example 2: Get maximum value in array JavaScript by comparison using negative input

``````var arr = [-12, -23, -44, -54, -19, -78];
var max = arr;
for (var k = 1; k < arr.length; k++) {
if (arr[k] > max) {
max = arr[k];
}
}
console.log("The highest value is: " + max);``````

Output:-

The highest value is: -12

This brings us to the end of the article, where we learned about JavaScript get highest value in array. Also see:- How to Remove from Set JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!