Transcript for:
JavaScript forEach Method Overview

the array for each method executes a given function on every elements from an array this sounds complicated so let's see how it works in code we have a numbers array containing all the numbers from 1 to 5 and let's say we want to console log these numbers to the console we can call the for each method which requires a callback function that callback function will be given three parameters the value but the current step on the loop the index and the array which is called upon so let's create a function and give it here on the for each as a callback function let's call it console item or something like that so we get the item get the index and we get the array usually you don't really work with the array but well it's good to know that we receive it and here we can do console.log item let's pass in now this function as a callback here and see what happens look at that we have one two three four and five we also get the index so maybe let's say something like a of item plus closing is equal to something like index let's see what this does we can say that a01 oh it's the other way around so a of index is equal to item okay my back so we can see now a of 0 so the element at index 0 it's 1 element at index 1 is 2 and so on and so forth now we can also provide this function we can actually write it as an arrow function so let's do that i'm going to copy this and put it inside here inline and convert this to an arrow function and now it should do the same thing but it's here in line usually this is how we'd write a function for the forage especially if this function is not called again let's also cancel that logged array so you can see what the array stands for basically is the array on which is called upon let's see also some other ways we can use the forage method we can use it to calculate the sum of the numbers from the array so for that let's have a sum which will be initialized with zero and here in the for each we can call sum plus equals item so keep in mind that this is every item from the array starting from the first one we now don't need this because we're not using them we can also remove the parentheses because we're having an arrow function and let's cancel that log the sum save and look at that we have 15. all the numbers are added up we can even add 10 15 25 and the function now works the same we get 65 another use case for this method is let's see how many times a letter appears in an array so we're going to have an array of letters let's see a b a again b again c d and a again something like that and we're going to create an object which will keep track of the count of every letter so let's create an object here count and now inside of the for each callback method we're going to do something interesting we're going to do the following if the count of item if we have a property inside the object with the item meaning a b or c or d then what we want to do that count item value to be plus equal one so we can also do plus plus else it means that we don't have that property already so we can initialize it uh with one let's cancel a clock discount and see what we get oh my bad i called it upon the numbers array and we changed it so let's call it now you can see that we get nicely an object with four properties a b c d and we get 3 for a 2 for b one for c and one for d this is a nice use case of the for each method