DEV Community

Shubham Jadhav
Shubham Jadhav

Posted on

Array.forEach() Method in JavaScript ๐Ÿš€

How can we iterate array items through a loop in JavaScript ??

If you know the above question answer then give the answer in comment section.

Today we are learning the most important method in JavaScript.


โ€ข What is Array.forEach()
method ?
=> In which method we can
iterate array items through
a loop it's called
Array.forEach() method.

โ€ข Syntax of Array.forEach()
=> arr.forEach((item, index)
=> { // code here.. })

This method return a callback function.

โ€ข Example

example 1

โ€ข Explanation
(1) Let's create an array and give any name as per you but I gave 'arr' as name.
(2) Now, I used here in next step arr.forEach() method.
(3) As we know this method return a callback function and this has two parameters 'item' and 'index'as shown above example.
(4) The forEach method iterate all items like a for loop one by one.

โ€ข Advantages of forEach() method
(1) It has a clear syntax.
(2) the forEach method keeps the variables from the callback function bound to the local scope
(3) The forEach method is less error prone.

๐Ÿ™ Thank you for reading...

Oldest comments (0)