Skip to content

How to Use JavaScript forEach() Method

How to Use JavaScript forEach() Method. The forEach() javascript method is useful to loop over all elements of array in JavaScript

The JavaScript forEach() method run once for each element in an array. For example to navigate to array and perform any action on each array element. The JavaScript forEach() method is useful in this situation.

JavaScript forEach() method uses following syntax:

arrayName.forEach(function callback(currentValue, index, array) {
    // action on array element
    // action on array element
}[, thisArg]);

JavaScript forEach() Example:

Below is the example of JavaScript forEach() method. In this example first we intialize and games array with some element. After that print each array element to console using forEach() method.

Add the above code in an HTML file and access in the web browser. Now open the console in browser’s developer tool to view the results like below.

JavaScript forEach() Mehtod

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top