Getting max of numbers in Javascript.

Learn how to get the maximum of numbers.


max function in the Math object can be used to find the maximum of multiple numbers and array.

Math.max(1,2); //2
Math.max(1,2,3); //3

If any one of the argument passed cannot be converted to number then it returns NaN

Math.max('1', 0); //1
Math.max('one', 1); // NaN
Math.max(1, 2, 3, 4, 5, 'one', 1); // NaN

We can use Math.max to find the maximum of the array

var array = [1,2,3,4,5];
Math.max(...array);

If you have array inside array you need to flatten the array then apply the max function.

To flatten an array without knowing the depth you can refer here

If you find this helpful surprise 🎁 me here.

Share if you feel happy.

Follow Javascript JeepπŸš™ if you feel worthy.

Leave a comment

Design a site like this with WordPress.com
Get started