You are currently viewing 3 Best Ways to Find Maximum Value in an Array in JavaScript

3 Best Ways to Find Maximum Value in an Array in JavaScript

JavaScript Get Max Value in Array

We use a javascript array to store a collection of data in a single variable. There may be a situation where we need to find the maximum value in the array of data. So, in this article, we will see how to get max value in the javascript array. When working with arrays in JavaScript, it is one of the common operations. For example,

const studentsMarks = [90, 57, 68, 81, 75];

In the above example code, we have created an array called ‘studentsMarks‘ and stored the list of students marks in it. We may find the maximum marks in the ‘studentsMarks’ array.

So like this, we may want to find the maximum value in an array of sales data to determine the highest sales figure over a given period of time. when working with user input or form data, where we may need to validate that a value falls within a certain range or is not too large, and so on.

3 Ways to get max value in an array

There are many ways to achieve this in JavaScript, but here we’ll explore three methods to find the maximum value in an array:

1. Using the Math.max() method with the spread operator

In this method, we use the spread operator to spread the array elements into separate arguments. The Math.max() returns the largest of the numbers given as input parameters.

We can pass the array elements as arguments to the Math.max() method by using the spread operator and then we get the maximum value easily, like:

const studentsMarks = [90, 57, 68, 81, 75];
const max = Math.max(...studentsMarks );

document.write(max) 
//Output : 90
  • In this example, we have created the array ‘studentsMarks‘ and it contains a list of student marks in integers.
  • To find the maximum value, we use spread syntax like “ …studentsMarks” to pass the array elements as arguments to the Math.max() method.
  • Then Math.max() method returns the largest argument, which is the maximum value in the array.
  • The maximum value is stored in the variable ‘max’. So, we print the ‘max‘ variable using document.write() method.
READ ALSO  Last Index of JavaScript

2. Using a for loop

In this method, we use a for loop to iterate through the array and compare each element with the current maximum value. If the current element is greater than the current maximum value, we update the maximum value.

const studentsMarks = [70, 57, 68, 81, 75];
let max = studentsMarks[0];
for (let i = 1; i < studentsMarks.length; i++) {
  if (studentsMarks[i] > max) {
    max = studentsMarks[i];
  }
}

document.write(max)

//Output : 81
  • In this example code, we have created the array ‘studentsMarks’.
  • To find the max student mark value, we created the variable ‘max‘ and initialized it to the first element of the array ”studentsMarks’.
  • Then we create for loop that iterates through the array starting from the second element.
  • Inside the loop, we check if the current element is greater than the current maximum value using ‘if statement‘. If it is, the maximum value is updated to the current element.
  • At the end of the loop, the maximum value is stored in the variable max. So, we print the ‘max’ variable using document.write() method.

3. Using the apply() method with Math.max()

The apply() method allows us to call a function with a given value and arguments provided as an array. We can use the apply() method to call the Math.max() method with the array elements as arguments.

const studentsMarks = [70, 57, 68, 81, 95];
const max = Math.max.apply(null, studentsMarks );

document.write(max)

//Output :95
  • In this example code, the variable ‘studentsMarks’ contains an array of integers.
  • Then we call apply() method on the Math.max() method with a ‘null‘ value and the ‘studentsMarks ‘ as an argument.
  • The apply() method calls the Math.max() method with the ‘studentsMarks’ elements as arguments. Math.max() method returns the largest argument, which is the maximum value in the array.
  • Finally, the variable ‘max‘ contains the maximum value.
READ ALSO  Top 3 Ways to Set Value of Textarea in JavaScript Dynamically

Conclusion

So, you can use the above methods to find the maximum value in an array. I recommend the for loop method because we don’t use any in-built method in it. By understanding these methods, you can confidently find the maximum value in any array you encounter

Leave a Reply