Get the maximum value in an array in JS
//取出数组中最大的值 var arrs = [11,152,21,5,31,23,90,102]; /* 1、定义一个变量num 2、让num的初始值是等于数组的第一个值 3、拿num去和数组中的每一个值做比较,如果num小于arr[i],那就让num = arr[i] 4、开始动手实现 */ function max(arr){ var num = arr[0]; for(var i=0;i<arr.length;i++){ if(num < arr[i]){ num = arr[i] } } return num; } console.log(max(arrs))//152
The above is the detailed content of How to get the largest value in an array using JS. For more information, please follow other related articles on the PHP Chinese website!