Javascript max() function for 3 numbers

前端 未结 5 2215
一整个雨季
一整个雨季 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:05

    In almost any language, one can use max twice:

    Math.max(num1, Math.max(num2, num3))
    

    As @CMS points out, JavaScript specifically allows an arbitrary number of parameters to Math.max:

    Math.max(num1, num2, num3);
    

提交回复
热议问题