Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript forEach Loop with Callback Function</title> </head> <body> <script> // Sample array var numbers = [1, 2, 3, 4, 5, 6]; // Loop over array and squaring each value numbers.forEach(function(value, index, array){ array[index] = value * value; }); console.log(numbers); // Prints: [1, 4, 9, 16, 25, 36] </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>