Understanding JavaScript Math.max() Object with Examples

By Digamber Rawat Last updated on
JavaScript Math.max() method returns the highest numerical value passed into it. It will return NAN if passed non-numerical value. Max is a static method for Math, that is the main reason we use it along with Math.max(). If we don’t pass any parameter in this method it returns null value. This method takes only numbers if you pass non numerical value then it will return null value.

Math.max(9, 5, 33, 29, 22);

// output: 33

Let us assume you have an array of numbers, then in this condition, we have 2 ways to work with Math.max(). Function.prototype.apply() and ECMAScript spread operator method. Check out the examples below in which i am trying to get the highest number using Math.max() method.

In first method we will use Function.protoype.apply() method.

let num = [12, 32, 41, 15, 26, 37, 48, 59];

Math.max.apply(null, num)
// output: 59

JavaScript Math.max() Method Syntax

Math.max(a1, a2, a3, ..., aX)
Argument Detail
a1, a2, a3, …, aX Include values here, in order to get the highest value among other numerical values.

Math.max() with Numerical Array using Spread Operator

Thanks to ECMAScript 2015, It can easily be done with ECMAScript spread operator. Check out the solution below.

let num = [12, 32, 41, 15, 26, 37, 48, 59];

Math.max(...num)
// output: 59

The new ECMAScript spread operator expands the values into the method’s parameter.

JavaScript Max Method Examples

let a = Math.max(6, 13, 9);
// output: 13

let b = Math.max(10, -5, 120, 120, 298);
// output: 298

let c = Math.max(1, 22, 9);
// output: 22

let d = Math.max(-1, -12);
// output: -1

let e = Math.max(4.2, 5.2);
// output: 5.2

Math.max() Browser Support

Chrome Firefox Edge Safari Opera
Yes Yes Yes Yes Yes

Full browser compatibility report on MDN Web Docs

Digamber Rawat
Digamber Rawat

Full stack developer with a passion for UI/UX design. I create beautiful and useful digital products to solve people’s problem and make their life easy.

Hire Me