What is the difference between "forEach", "for...in" and "for...of"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is the difference between "forEach", "for...in" and "for...of"?

My english is not helping me and the translation is too bad... 😅 https://codeburst.io/javascript-the-difference-between-foreach-and-for-in-992db038e4c2

8th Aug 2019, 1:56 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
5 Answers
+ 14
for in is getting the keys of an object, index if it is array. https://code.sololearn.com/WyWx8udHdDjn/?ref=app for of is getting the array elements https://code.sololearn.com/WX2CwmYR8lIo/?ref=app forEach is an Array method which accepts an argument which is a function with element and index as parameter. https://code.sololearn.com/WbfJmVR5cCsA/?ref=app
8th Aug 2019, 3:59 PM
Gordon
Gordon - avatar
+ 3
Alex Tusinean The forEach() method calls a function once for each element in an array, in order. In other words - forEach element of the array, pass that element to a function. https://www.w3schools.com/jsref/jsref_foreach.asp https://code.sololearn.com/Wcik1RThrFeu/#js
8th Aug 2019, 8:13 PM
ODLNT
ODLNT - avatar
+ 2
I understand now what is for...in , and for...of, but I still don't understand forEach. 😅
8th Aug 2019, 5:23 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 2
JS is getting quite cryptic these days, isn't it?
8th Aug 2019, 6:00 PM
HonFu
HonFu - avatar
+ 1
forEach is a method from array.prototype, this method accept a argument, an callback function with tow argunents: item element and index element. This method is almost same at map method for...in and for...of are the control structure syntax, this syntax allow iterate an array, the diference is in for...in return the index and for...of return the element https://code.sololearn.com/Wi71QLk9Igi7/?ref=app
11th Aug 2019, 10:52 PM
Ramón Rivas