运行 ❯
获取您
自己
的网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Arrays</h1> <h2>The forEach() Method</h2> <p>forEach() calls a function for each element in an array:</p> <p>Compute the sum of the values in an array:</p> <p id="demo"></p> <script> let sum = 0; const numbers = [65, 44, 12, 4]; numbers.forEach(myFunction); document.getElementById("demo").innerHTML = sum; function myFunction(item) { sum += item; } </script> </body> </html>