Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be using the for-loop construct or the forEach method for iteration?

Tags:

javascript

Is one way more efficient? Are there any limitations in one method versus the other? Thanks for any input you guys can give me ;-)

like image 549
Dutch Avatar asked Jan 22 '23 22:01

Dutch


1 Answers

The Array.prototype.forEach method is part of the ECMAScript 5th Edition Specification, is available on all browsers but not in IE.

I would suggest you to use a simple sequential for loop, IMO is more efficient since forEach needs a callback function, a function will be created starting a closure and you don't need to care about IE.

But if you want to use forEach and all the other new Array.prototype methods such as map, filter, every, some, etc... you can add an implementation for IE, in the pages I linked.

like image 153
Christian C. Salvadó Avatar answered May 23 '23 19:05

Christian C. Salvadó