{"id":4824,"date":"2024-06-13T17:05:45","date_gmt":"2024-06-13T11:35:45","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=4824"},"modified":"2024-06-13T17:05:46","modified_gmt":"2024-06-13T11:35:46","slug":"difference-between-for-for-of-for-in-and-foreach","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/difference-between-for-for-of-for-in-and-foreach\/","title":{"rendered":"Difference between For, For Of, For In and ForEach"},"content":{"rendered":"<p><\/p>\n<p>What loop should I use, why are there so many?<\/p>\n<p><\/p>\n<p>In this article, I am going to give you my view of what loop you should use and why I see them as beneficial and I hope this means you can push on with your work and avoid the deeper conversation until a later stage.<\/p>\n<p><\/p>\n<p>For Loop<\/p>\n<p><\/p>\n<p>We are all familiar with this one, the first loop most tutorials teach us, which is surprising as it&#8217;s not the simplest to read but we all know it and I\u2019m willing to bet that if you are starting then this will be your go-to loop.<\/p>\n<p>Which is&nbsp;fine it does the job perfectly well.<\/p>\n<p>So for the sake of clarity here is an example:<\/p>\n<p><\/p>\n<p>const arr = [1, 2, 3, 4, 5];<\/p>\n<p>for (let i=0; i &lt; arr.length; i++) {<\/p>\n<p>console.log(arr[i])<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>For loop can iterate in reverse:<\/p>\n<p><\/p>\n<p>for (let i = arr.length &#8211; 1; i &gt;= 0; i&#8211;) {<\/p>\n<p>console.log(arr[i])<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>That said I\u2019ve never needed to iterate over something in reverse! So let&#8217;s look at For Of and For In next:<\/p>\n<p><\/p>\n<p>For Of and For In<\/p>\n<p><\/p>\n<p>These loops are the ones you should use as your everyday go-to loops. The key reasons for this are:<\/p>\n<p><\/p>\n<p>Clean and readable syntax You can use \u2018Break\u2019 and \u2018Continue\u2019 like you would in the traditional For Loop. It pretty much loops over anything.<\/p>\n<p><\/p>\n<p>What is the difference between \u2018of\u2019 and \u2018in\u2019?<\/p>\n<p><\/p>\n<p><\/p>\n<p>For Of: It loops over the Values<\/p>\n<p>For In: Loops over the Keys.<\/p>\n<p><\/p>\n<p>const arr = [1,2,3,4,5]<\/p>\n<p>arr.six = 6;<\/p>\n<p>arr.seven = 7;<\/p>\n<p>\/\/This array will now look like [1,2,3,4,5,six:6,seven:7]<\/p>\n<p>\/\/ OF: iterates over values<\/p>\n<p><\/p>\n<p>for (const value of arr ) {<\/p>\n<p>console.log(value);<\/p>\n<p>}<\/p>\n<p>\/\/ Logs:<\/p>\n<p>\/\/ 1<\/p>\n<p>\/\/ 2<\/p>\n<p>\/\/ 3<\/p>\n<p>\/\/ 4<\/p>\n<p>\/\/ 5<\/p>\n<p><\/p>\n<p>\/\/ IN: iterates over keys<\/p>\n<p><\/p>\n<p>for (const key in arr ) {<\/p>\n<p>console.log(key, arr[key])<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>\/\/ Logs:<\/p>\n<p>\/\/ &#8216;0&#8217; 1<\/p>\n<p>\/\/ &#8216;1&#8217; 2<\/p>\n<p>\/\/ &#8216;2&#8217; 3<\/p>\n<p>\/\/ &#8216;3&#8217; 4<\/p>\n<p>\/\/ &#8216;4&#8217; 5<\/p>\n<p>\/\/ &#8216;six&#8217; 6<\/p>\n<p>\/\/ &#8216;seven&#8217; 7<\/p>\n<p><\/p>\n<p><\/p>\n<p>We can also iterate over an array of objects:<\/p>\n<p><\/p>\n<p>const numbers = [{one:1},{two:2},{three:3},{four:4}];<\/p>\n<p>for (const number of numbers ) {<\/p>\n<p>console.log(number);<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>\/\/Logs:<\/p>\n<p>\/\/ { one: 1 }<\/p>\n<p>\/\/ { two: 2 }<\/p>\n<p>\/\/ { three: 3 }<\/p>\n<p>\/\/ { four: 4 }<\/p>\n<p><\/p>\n<p>And even iterate over a string :<\/p>\n<p><\/p>\n<p>const str = &#8216;string&#8217;;<\/p>\n<p>for (const letter of str) {<\/p>\n<p>console.log(letter)<\/p>\n<p>}<\/p>\n<p>\/\/Logs:<\/p>\n<p>\/\/ &#8216;s&#8217;<\/p>\n<p>\/\/ &#8216;t&#8217;<\/p>\n<p>\/\/ &#8216;r&#8217;<\/p>\n<p>\/\/ &#8216;i&#8217;<\/p>\n<p>\/\/ &#8216;n&#8217;<\/p>\n<p>\/\/ &#8216;g&#8217;<\/p>\n<p><\/p>\n<p>As mentioned earlier these loops work with \u2018break\u2019 and \u2018continue\u2019 just like the traditional For Loop, which becomes extremely valuable in keeping your code efficient:<\/p>\n<p><\/p>\n<p>for (const letter of string) {<\/p>\n<p>if(letter === &#8216;t&#8217;) break<\/p>\n<p>console.log(letter) }<\/p>\n<p>\/\/ Logs only the first letter &#8216;s&#8217;<\/p>\n<p><\/p>\n<p><\/p>\n<p>Errors:<\/p>\n<p>If you attempt to use for&#8230;of with an object that is not iterable, it will throw an error. On the other hand, for&#8230;in can still be used to loop over the properties of any object, regardless of whether it is iterable or not.<\/p>\n<p><\/p>\n<p>Performance:<\/p>\n<p>for&#8230;in can be slower than for&#8230;of due to the extra steps involved in property enumeration. However, the impact on performance is generally negligible unless you are working with a large number of properties.<\/p>\n<p><\/p>\n<p>As we can see, the benefits stand out without highlighting them further. So what about forEach() why am I switching my allegiance to For Of?<\/p>\n<p><\/p>\n<p>array.forEach()<\/p>\n<p>The main drawback with forEach() apart from only being of use on arrays, is you can\u2019t stop it. Unlike above where once a condition is met we can enter a \u2018break\u2019 clause and the loop would end, forEach() doesn\u2019t work like that.<\/p>\n<p>In the case where you may handle 10000+ rows of data, what happens when we find the result in the first few 100 rows?<\/p>\n<p>Well, nothing we have to sit and wait for the loop to iterate through every row before it finishes.<\/p>\n<p>It is at this point I begin to understand why this does become a discussion about performance and I begin to understand why people are so vocal on the topic.<\/p>\n<p><\/p>\n<p>\u201cbut couldn&#8217;t I just return out of the loop?\u201d Let&#8217;s see what happens when we try:<\/p>\n<p><\/p>\n<p>const array = [1,2,3 &#8230; 567890];<\/p>\n<p>array.forEach((num)=&gt; {<\/p>\n<p>console.log(num)<\/p>\n<p>}<\/p>\n<p>\/\/ Logs every number up to 567890<\/p>\n<p><\/p>\n<p>array.forEach((num)=&gt; {<\/p>\n<p>if(num === 1001) return<\/p>\n<p>console.log(num)<\/p>\n<p>}<\/p>\n<p>\/\/ Logs every number&nbsp;up to 1000, but don&#8217;t be fooled<\/p>\n<p>\/\/It is still going to loop through the remaining entries!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What loop should I use, why are there so many? In this article, I am going to give you my view of what loop you should use and why I see them as beneficial and I hope this means you can push on with your work and avoid the deeper conversation until a later stage.<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[231,333,334,172,263],"tags":[330],"class_list":["post-4824","post","type-post","status-publish","format-standard","category-article","category-asynchronous-javascript","category-best-practices","category-javascript","category-javascript-frameworks","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=4824"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4824\/revisions"}],"predecessor-version":[{"id":4828,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4824\/revisions\/4828"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}