Posts

Showing posts from December, 2019

Comparing for Loop with ES6 forEach Method

Image
J avaScript is a client-side scripting language (forget Node.js). When writing code, we should be careful because our code is supposed to run on all devices rather than just gaming laptops or MacBooks. We should not give much load to the browser. ES6 a.k.a ES2015 introduced a lot of features especially the array functions forEach , map , filter , etc. Many people believe forEach is an alternative for the for loop. Is that true? Let’s check some facts. Before starting, let’s check what is for loop and what is forEach. For Loop For is a keyword, to create looping statements, just like while and do…while. JS engine can directly understand what it is. forEach forEach is a function defined in Array.prototype , just like all other functions. The engine can’t understand it directly. It needs to get the function definition in order to execute it. For vs forEach Choosing one over another will have huge impact on performance. When you have to deal with an array o