Max Array Value
Problem
You need to find the largest value contained in an array.
Solution
You can use Math.max() JavaScript method along with splats.
Alternatively, it’s possible to use ES5 reduce
method. For backward compatibility with older JavaScript implementations, use the above.
Discussion
Math.max
compares every argument and returns the largest number from arguments. The ellipsis (...
) converts every array value into argument which is given to the function. You can also use it with other functions which take variable amount of arguments, such as console.log
.