# forEach loop question

So they say forEach loop makes writing a for loop easier. But the problem is I can’t see how it works . I mean I am trying to compare it with a for loop and watching easy explained videos there is no one.
Can you explain or link to an existing easy explained video tutorial please?

The `.forEach()` loop is most common used to iterate(loop through) an array. What it essentially does is that it grabs the array, then it will loop through every single item inside the array. Then FOR EACH item, you can do something with it using a function. ex:

``````let numbers = [1, 2, 4, 3, 5, 6, 9, 7];
let numbersUnderSix = [];

//it will iterate through every item in the array `numbers`, then for each
// items its going to go through this function that takes the number
// then checks if the number if under 6, if it is, then it's gonna get pushed
// to the variable `numbersUnderSix`
numbers.forEach(function(num) {
if(num < 6) {
numbersUnderSix.push(num);
}
});

console.log(numbersUnderSix); //outputs [1, 2, 4, 3, 5]
``````

how is it connecting to the numbers?
1)
we are using the variable => numbers.
2)
then dot notation
numbers.forEach()
Here the .forEach() method is calling a function it can be anonymous or regular function with a name, right?
3) What is going thro an the array ? What is connecting the array to forEach and what exactly grabs the the array here? İs it the forEach method or is it the function in it?

Usually in tutorials what they can’t teach is show what connects all these together.
They just talk in videos without explaining what connects, grabs, and how all of these work together.

It is connecting to the numbers because I asked it to go through the variable `numbers` using the `numbers.forEach()`, So anything before the `.` will be the one being iterated through.

Yes, you can call in a predefined function that will do something to it, but most of the time its going to be an anonymous inline function.

• I see what you’re trying to ask here. In `for` loop, it uses the dummy variable like `i` and others. But in `.forEach()` it doesn’t use that, when you use it, the function will always think that you are giving it an array to loop through. It’s simply just going through EVERY SINGLE item in the array you’ve given. The `.forEach()` grabs an array when you are declaring it. So when you attach something right before the `.(period)`, that is what it’s going to take in. So like my example above, `numbers.forEach();`, it will grab `numbers`

As you can see in my example, that it goes through all the numbers inside the variable

1 Like

Yes, they do this, because they think you already know that if there is a `.(period)` on a method, that means its going to grab whatever is before it, then anything in the `()` is going to do something that will affect another piece of code.

Like `.split();`, it grabs the array before the `.`

1 Like

Okay Now it’s a little easier to understand. Now I have to watch the tutorials again and read your explanation. I wish you had a youtube channel. You are being so patient with me. So thanks lol

1 Like

Nice to know I helped. I just like helping people, if they need some help and I can help them, why can’t I help them? Anyways, have a nice day and,

Happy Coding !!

1 Like

Should’t they teach that or at least mention what is connecting all and how it works?
The best tutorial would be explaining something in its purest form. how it works etc.
Then they can throw any exercise or iteration they want.
I am taking Kalob Taulin’s course on udemy
he has something like this

``````var names = ["name 1", "name 2", "name  3", "name 4"];
names.forEach(function(name, index) {
console.log(name, index);
})
``````

That explanation with image is really to understand. Even better than watching videos.

They don’t really do this, because typically people that is learning this type of methods already knew the basics of how parts of the code is going to work. That’s why documentations are always GREAT. I used MDN and DevDocs.

• MDN:
• DevDocs (I prefer because of how the UI Design looks):
1 Like

Thanks man! I don’t know if you are a man or a woman. But thanks anyway.
An sorry If I wasted your time by asking a lot of questions.
Do you have a coding youtube channe?

1 Like

If you read my profile, I’m a 15 yo guy.

It’s fine, that’s what forums are for, asking questions. I’m here because I have free time anyway, and it looks like it was spent the right way.

Unfortunately not…

Everyone on youtube says mozilla dev site is good but it has a hard navigation to follow. Every time I click on a link the navigation entirely changes.
I don’t know how people can use such a websit that changes the nav every tşme you click on a link. But thanks for links, explanation.

That’s why I like devdocs. It has a really nice UI, and can be used Offline

1 Like

For a 15 year old you are really good lol. I am 25 having hard time grasping concepts.

1 Like