Dear JavaScript Aficionado,
Many people get confused about this array method because they focus on the word “for” in the name and somehow overlook the word “each.” By focusing on the former, they quickly associate it with the well-known “for” loop.
But if we include the word “each,” it becomes pretty clear what it does – it ALWAYS iterates over every element of an array. Even if you use “return” to skip some code execution in the callback function, the array element still needs to be visited.
So if you deal with big arrays and you have to think about optimizing stuff like the “time complexity” of your code, then you will need to use a traditional “for-of” loop because it will allow you to “continue” and/or “break” and thus – don’t visit elements unnecessary.
I hope this short discussion was helpful to you!
Let’s advance together,
Sashe
Freelancer (web developer)
