


Why using return inside the forEach() loop causes performance issues?.Use return to prevent running undesired logic (alternative solution).That's all I wanted to cover in this video. So if you simply want to iterate throughĪn array to access its elements, you can use any of these loops to do so. Of social array, log the channel value on the console and that's it.

So all we are saying here is for every channel Here, you simply need to declareĪ variable channel, which can be const because you are not modifying the channel variable. Unlike the simple for loop, we looked at the for-of loop came from ES6 specification. Gives you the direct access to the element in an iterable. Now the third approach would be to use for-of, which When this function executes, it logs every channel on the console. So we pass through this function an argument channel, which is nothing but the current One of the parameters of this function is the current element in the array. To use foreach, you need to pass a function as an argument, which will be executed on each element of Now, the second approach is to use foreach() method available on arrays. Let's run the code and you can see it logs every element in this array. So we will use console.log() to print on the console, each element at index x from the social array. Writing for var x=0, because you would like to iterate from 0th index and go until the last element of the array, which can be determined using length property of the socialĪrray, increment the index each time by one,

The first approach is to use for loop in JavaScript. Now our goal is to loop through every element in the social array and print that element. We declare it and add the values, Twitter, Instagram, Facebook, LinkedIn, and Snapchat. We have this array containing the names of social channels. I'm using Chrome snippets in my browser to write the code, but feel free to write it in your favorite editor or on theīrowser console. It contains the name of few social channels. Let's use the social array for iteration. We will look at a couple of ways to loop through an array in JavaScript.
