What is Array ForEach?
Array ForEach is a method that exists on the Array.prototype
that was introduced in ECMAScript 5 (ES5) and is supported in all modern browsers.
Array ForEach is the entry-level loop tool that will iterate over your array and pass you each value (and its index). You could render the data to the DOM for example. ForEach, unlike other array methods, does not return any value.
Think of Array ForEach as: "Give me my array values one-by-one so I can do something with them!"
Here's the syntax for Array ForEach:
array.forEach((value, index, array) => {...}, thisArg);
ForEach is also considered a "modern for...in loop" replacement, despite behaving differently and having less flexibility.
ForEach doesn't allow us to break
the loop or even loop backwards, which may be a common scenario you'll run into - so it's certainly not a flat-out replacement. However, it's a pretty standard approach nowadays.
Read my full article on Ultimate Courses blog
Top comments (0)