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?
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?
Using with the new spread operator
for more info
In almost any language, one can use max twice:
As @CMS points out, JavaScript specifically allows an arbitrary number of parameters to Math.max:
Push your values into an array
arr
and useMath.min.apply(null, arr)
orMath.max.apply(null, arr)
for maximum and minimum values respectively:The Math.max function can accept any arbitrary number of arguments:
Syntax:
Usage:
For example:
You could even use it to get the maximum value of an array of numbers with the help of apply:
If you can target ES6 (ES2015), you can also use the spread operator: