JS Get the maximum value of an array in one sentence

 Usually we will use the first method, but there is a very simple method: Math.max.apply(null, arr)

const arr = [16,2,4,32,6,7];
function max(arr) {
  let maxVal = arr[0];
  for(let i of arr) {
    maxVal = maxVal>i?maxVal:i;
  }
  return maxVal;
}

console.log(max(arr));// 32

console.log( Math.max.apply(null,arr) ); // 32

 

Guess you like

Origin blog.csdn.net/qq_38238041/article/details/88867997