要获得数组中最大的数字,可以尝试运行以下代码。它返回最大的数字530-
<!DOCTYPE html> <html> <body> <script> var myArr = [340, 25, 530, 299]; var max = myArr.reduce(function(a,b){ return (a > b) ? a : b; }); document.write("Largest number in the array: "+max); </script> </body> </html>
输出结果
Largest number in the array: 530