3 ms·
Use .forEach
- declandewet 13y agoThe reasons we don't are pretty good ones. The first, and most important reason, is that native implementations of methods like `.forEach` are extremely slower than their native-for-loop equivalents. For casual side projects, this does not really matter, and some might argue that in newer browsers the efficiency loss is minimal. But when you're developing a particularly JavaScript heavy client-side application, predominantly use .forEach, and your users will notice. The second reason is browser compatibility. `.forEach` is not supported in Internet Explorer 7 or 8. That is an extremely large chunk of potential users. All that being said, there are definitely ways to gain the same functionality using a utility library like Underscore (or the much-faster-than-underscore Lodash). Instead of `array.forEach` it's simply `_.forEach(array, function(item) { });` - http://lodash.com/docs#forEach http://lodash.com/docs#forEach
- workhere-io 13y agoThis one is even more concise and avoids the scoping issues that forEach have: for (var i in cars) { console.log(cars[i]) } Also, if you're using JS on the frontend, forEach isn't supported by all browsers: http://stackoverflow.com/questions/156696/which-web-browsers-natively-support-array-foreach http://stackoverflow.com/questions/156696/which-web-browsers...