Ufuk's Blog

forEach array helper method

☕️ 1 min read

We all know for loops, they are great tools to repate an action. With ES6 we now have great methods to do the same job with less of a pain.

const arr = ['red', 'green', 'blue', 'yellow'];

// this is the classic way of iterating over an array
for (let i; i < arr.length; i++) {
  console.log(arr[i]);
}

// this is the modern way of iterating over an array
arr.forEach(item => console.log(item));

forEach method gives us an option to iterate over an array with less logic. Less logic means less code, less code means less possible mistakes to do.