Iterator methods
JavaScript also has some built in methods as part of the Array API that are extremely useful in the day-to-day coding tasks. These methods accept a callback function that we can use to manipulate the data in the array as needed.
Let's look at these methods. Consider the following array used as a base for the examples in this section:
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Iterating using the forEach method
If we need the array to be completely iterated no matter what, we can use the forEach
function. It has the same result as using a for
loop with the function's code inside it, as follows:
numbers.forEach((value, index) => {
console.log(`numbers[${index}]`, value);
});
Most of the times, we are only interested in using the value coming from each position of the array, without having to access each position as the preceding example. Following is a more concise example:
numbers.forEach(value => console.log(value));
Depending on personal...