From the course: JavaScript: Arrays

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

forEach for ease and readability

forEach for ease and readability - JavaScript Tutorial

From the course: JavaScript: Arrays

forEach for ease and readability

- [Instructor] Iterating over arrays is critical to working with JavaScript. For many of us, when we first started learning JavaScript, we were taught how to use a for Loop to iterate over each item in an array. As you can see, this can get a bit verbose. Luckily, array methods like forEach were created to make the process of array iteration more clean and succinct. The forEach method executes a function once for each element in an array. When using a for Loop, you would have to explicitly reference the length of the array to ensure that the entire dataset was looped over. Additionally, forEach does not mutate the original array. This means that the original array you were looping over will not change. The function executed on each element in the array is a callback function. This function has three parameters. The first is currentValue, and it is required. This represents each value within the array you are…

Contents