

In the code below, the console statement in the forEach() block, displays the contents in the array even though the ‘element’ variable name is reused. Therefore, if you reused a variable declared outside the forEach() statement inside forEach(), that variable would not be affected. Using a forEach() statement allows you to maintain the variable scope within the callback function. If you want to maintain the scope within the block. While forEach() and the for loop can provide the same output, there are a couple of things you should consider before choosing one of them to use in your code. It logs each element in an array on the console as a forEach() statement would. It is worth mentioning that one of the most common methods used for iteration in JavaScript is the for loop. You could therefore rewrite the above function as follows: let arr1 = 'some' works the same way as every only that it exits the loop if the function returns true. let arr1 = Īnother alternative would be to use the ‘some’ keyword. The code below searches for the number 2 in the array by returning false when it is found. Instead, you could use ‘every’ which tests every element against a function and returns either true or false. However, using 'break' in a forEach() statement doesn’t work and would throw a syntactic error. For instance, if you were searching an array and wanted to exit the loop when you found it, you could think of using the break keyword. Unfortunately, forEach() does not provide a direct way of breaking out of a loop. When using loops, you might need to break out of the loop. The following code displays the items in an array. You could instead choose to define a new function and pass it to the forEach() method as an argument. In the examples above, you are defining the function inside the forEach statement.

let array1 = Įxample-5: Refactoring the callback function Consider the following code that increments each item in the array by 1. The callback function passed into the forEach() statement can be used to modify an array. let items = Įxample-3: Tracking the index of an element in the arrayĪs mentioned earlier in the article, the callback function of the forEach() statement accepts an optional parameter representing the index of each element in the array.Ĭonsole.log(`index of $)Įxample-4: Modifying an element from an array If you are looking to print all the elements in the array, you need to log each element on the console. let array1 = Įxample-2: Displaying all the items in an array You will first need to declare an empty array, then add each element from the original array to it. You can use the forEach() statement to copy the elements of one array to another. This method always returns undefined() and does not mutate the array it is iterating over.īelow are some practical examples of how you can use forEach()Įxample-1: Copying items from one array to another The forEach statement calls the callback function for each element in the array in ascending order and only once.

Array - An optional array object being traversed.Index - An optional value indicating the index of the current element.

