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

后端 未结 4 1876
太阳男子
太阳男子 2020-11-28 13:05

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 eve

4条回答
  •  误落风尘
    2020-11-28 14:01

    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));
    

提交回复
热议问题