Why is math.max() returning NaN on an array of integers?

JavascriptArraysMathMaxNan

Javascript Problem Overview


I am trying to get the highest number from a simple array:

data = [4, 2, 6, 1, 3, 7, 5, 3];

alert(Math.max(data));

I have read that if even one of the values in the array can't be converted to number, it will return NaN, but in my case, I have double-checked with typeof to make sure they are all numbers, so what can be my problem?

Javascript Solutions


Solution 1 - Javascript

The reason why your code doesn't work is because Math.max is expecting each parameter to be a valid number. This is indicated in the documentation as follows:

> If at least one of arguments cannot be converted to a number, the result is NaN.

In your instance you are only providing 1 argument, and that 1 value is an array not a number (it doesn't go as far as checking what is in an array, it just stops at knowing it isn't a valid number).

One possible solution is to explicitly call the function by passing an array of arguments. Like so:

Math.max.apply(Math, data);

What this effectively does is the same as if you manually specified each argument without an array:

Math.max(4, 2, 6, 1, 3, 7, 5, 3);

And as you can see, each argument is now a valid number, so it will work as expected.

Spreading an array

You can also spread the array. This essentially treats the array as if each item is being passed as it's own argument.

Math.max(...data);

Solution 2 - Javascript

if you see doc for Math.max you can see next description

>Because max() is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created (Math is not a constructor). > >If no arguments are given, the result is -Infinity. > >If at least one of arguments cannot be converted to a number, the result is NaN.

When you call Math.max with array parameter like

Math.max([1,2,3])

you call this function with one parameter - [1,2,3] and javascript try convert it to number and get ("1,2,3" -> NaN) fail.
So result as expected - NaN

NOTE: if array with just one number - all work correctly

 Math.max([23]) // return 23

because [23] -> "23" -> 23 and covert to Number is done.


If you want get max element from array you should use apply function, like

Math.max.apply(Math,[1,2,3])

or you can use the new spread operator

Math.max(...[1,2,3])

Solution 3 - Javascript

It's not working because you are passing an array as the parameter instead of comma separated numbers. Try spreading the array like this:

data = [4, 2, 6, 1, 3, 7, 5, 3];    
alert(Math.max(...data));

Solution 4 - Javascript

If you have to use the Math.max function and one number of the array might be undefined, you could use:

x = Math.max(undefined || 0, 5)
console.log(x) // prints 5

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt WelanderView Question on Stackoverflow
Solution 1 - JavascriptmusefanView Answer on Stackoverflow
Solution 2 - JavascriptGrundyView Answer on Stackoverflow
Solution 3 - JavascriptBijoy ThangarajView Answer on Stackoverflow
Solution 4 - JavascriptdpolicastroView Answer on Stackoverflow