Member-only story
Arrays are one of the most fundamental data structures used in nearly every JavaScript program. And modern JS makes working with arrays easy with a host of powerful built-in methods.
In this deep dive, we will explore some of JavaScript’s versatile array methods — from basics like foreach and map to advanced methods like reduce. Understanding these methods unlocks the ability to transform, filter, and manipulate array data effortlessly.
Let’s dive in!
Iteration Made Easy
The simplest way to iterate over an array in JavaScript is with a basic for loop:
let colors = ['red', 'blue', 'green'];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
While this works, looping explicitly like this gets tedious. Instead, we can use:
forEach
Iterates over an array without needing indexes:
colors.forEach(color => {
console.log(color)
})