Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
The forEach() method operates on arrays (including arrays of objects). It will iterate through all of the elements of an array. The forEach() method is the same as the map() method, except that that the forEach() method does not return any values.
Example of a forEach() method applied to an array of numbers (Run Example)
<!DOCTYPE html> <html> <head> <title>Course notes example code</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> let numbers = [1, 3, 6, 8, 11] numbers.forEach(number => console.log(number)) </script> </head> <body> <p>View the console output in browser's Web DevTools (F12)</p> </body> </html>
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.