forEach - don't understand, please, explain | SoloLearn: Learn to code for FREE!


forEach - don't understand, please, explain

In the forEach method, how does the interpreter understand that 'el' refers to the array index? В методе forEach, как интерпретатор понимает, что 'el' относится к индексу массива? var arr = [2, 3, 7, 8]; arr.forEach(function(el) { console.log(el * 2); });

5/25/2020 1:17:41 PM

Сергей Куликов

5 Answers

New Answer


• How to Use forEach() to Iterate an Array in JavaScript —


the first parameter el is not index. the first parameter of the callback function is the element. the optional second parameter is index. the optional third parameter is the array itself.


This is just how the syntax works. You have called the .forEach method on the arr object already. It tells the interpreter to iterate through the arr and assign its consecutive values to el variable.


Thank you all for the answers, I figured it out. I was confused by the function definition inside the forEach method. Спасибо всем за ответы, разобрался. Меня смутило определение функции внутри метода forEach.


It uses a for loop under the hood.