Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to 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>