The Math.max()
function returns the largest number of among parameters.
Math.max([value1[, value2[, ...]]]) // value1, value2, ... are Numbers.
If at least one of the arguments cannot be converted to a number, NaN is returned.
let a = Math.max(10, 20); // 20 console.log(a);// ww w. ja v a 2 s. c om a = Math.max(-10, -20); // -10 console.log(a); a = Math.max(-10, 20); // 20 console.log(a); let max = Math.max(3, 54, 32, 16); console.log(max); // 54
The following code uses array.reduce()
to find the maximum element in a numeric array:
var arr = [1,2,3]; var max = arr.reduce(function(a, b) { return Math.max(a, b); });/*from w ww .j av a2s.c o m*/ console.log(max);
The following code uses spread operator to get the maximum of an array:
var arr = [1, 2, 3]; var max = Math.max(...arr); console.log(max);//from w w w . ja v a2 s. c o m