DEV Community

Discussion on: Difference between for...of and for...in loop in JavaScript.

Collapse
 
nanythery profile image
Nadine M. Thêry

I was about to ask what's the difference between forEach, which I tend to use a lot and these other methods. Honestly, I use forEach because I learnt it first and I didn't feel need to learn others haha.
Are these methods a better practice or a refactor method?

Collapse
 
ddasb profile image
Damien Da Silva Bregieiro

For of came with ES2015 and is currently a better practice :

  • More readable
  • No callbacks
  • Faster (Refering to some benchmark but not sure)
  • You can also use .entries() and destructuring
Collapse
 
swastikyadav profile image
Swastik Yadav

Hey Nadine,

forEach is an Array method just like Array.prototype.map and Array.prototype.filter. The difference between forEach and other Array methods is that unlike "map" and "filter", "forEach" does not return the array it always returns undefined.

I hope this cleared the confusion.

Thanks for bringing this up in the comments. 😊