forEach Question... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

forEach Question...

Im currently learning javascript and I just cant seem to understand the forEach method... can someone pleeassseee tell me what this is in the most simplest way. and if u wouldn't mind give an example. Thank You!

18th Jan 2018, 2:17 AM
The Kid
The Kid - avatar
1 Answer
+ 2
This code: function f(item, index) {} var num = [3, 2, 1]; num.forEach(f); Is the same as: function f(item, index) {} var num = [3, 2, 1]; for(var i=0; i<num.length; i++) f(num[i], i); In both cases, f is called 3 times. It is called with: item index 3 0 2 1 1 2
18th Jan 2018, 2:50 AM
John Wells
John Wells - avatar