forEach one of the easiest and preferred replacement for our old and gold for loop which is the heart of most of the old school programmers

But know the time is changed and it is the key to survival to change with time and that’s what is followed here

In this tutorial we are gonna have a look at the great forEach of Javascript and also gonna work on a practical ground as well

If you don’t know that what is forEach and what the hell it does or you can’t remember what it is let’s make a power refresher

What is forEach

forEach is a new way of creating a loop in a more and easy manner with no time it is quite loved by the programmers nowadays and also hated by new programmers because they don’t like it but now i will try that you will understand the forEach completely

The Syntax

The syntax of forEach is quite simple it can take 3 arguments at a single time but only 1 is mandatory

Let’s see it To explain it i am creating an array of colors If you don’t know what an array is you can read my this post Here

//Declaring array colors//
var colors = ["red","blue","green"];

//Using forEach to get the array//
	colors.forEach(function (color,index,arr) {
		// body...
		console.log(color);
		console.log(index);
		console.log(arr)
	})

Now as you can see the syntax is not that much confusing it is quite simplified but let’s break it down a little bit for more clear vision

As stated in the code the forEach can hold three arguments

first one is the actual value the second is the index of that value in the array and the third is the array itself

Only the first argument is necessary other 2 are optional

It will be confusing but you will get used to it quite easily when you will use it on a daily basis i also recommend going through the MDN DOCS for more info

Wrapping UP

Maybe after reading this, you have a quite great understanding of how to use forEach and if you know like always the comment section is open for any questions and for criticism and appreciations as well