Get the Max Value of an Array with JavaScript

02/08/2022

Contents

In this article, I will introduce how to get the max value of an array with JavaScript.

The Math.max() method

If you specify multiple numbers as arguments, the Math.max() method returns the maximum value among them.

The syntax is below.

Math.max(num1,num2,num3,...)

To get the max value of the array that stores the numerical value, it is as follows.

Math.max.apply(null,array)

Get the Max Value of an Array

Below is the sample code to get the maximum value of the array.

console.log(Math.max(2, 5, 3)); // 5

console.log(Math.max(-2, -5, -3)); // -2

const array=[3,6,2,9,7];
console.log(Math.max.apply(null,array)); // 9