เพื่อหาตัวเลขที่ใหญ่ที่สุดในอาร์เรย์ คุณสามารถลองรันโค้ดต่อไปนี้ จะคืนค่าตัวเลขที่ใหญ่ที่สุดคือ 530:
ตัวอย่าง
Live Demo
<!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