Javascript max() function for 3 numbers

前端 未结 5 2208
一整个雨季
一整个雨季 2020-12-09 07:40

I need to find the highest number from 3 different numbers. The only thing I\'ve found is max() but you can only use 2 numbers.

Whats the best way?

5条回答
  •  既然无缘
    2020-12-09 08:03

    Push your values into an array arr and use Math.min.apply(null, arr) or Math.max.apply(null, arr) for maximum and minimum values respectively:

    var arr = [];
    arr.push(value1);
    arr.push(value2);
    arr.push(value3);
    
    var minValue = Math.min.apply(null, arr);
    var maxValue = Math.max.apply(null, arr);
    

提交回复
热议问题