{"id":5430,"date":"2023-08-09T00:13:17","date_gmt":"2023-08-09T05:13:17","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5430"},"modified":"2023-08-09T00:13:17","modified_gmt":"2023-08-09T05:13:17","slug":"3-different-ways-to-loop-through-arrays-in-javascript-in-2023","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/3-different-ways-to-loop-through-arrays-in-javascript-in-2023\/","title":{"rendered":"3 different ways to loop through arrays in JavaScript in 2023"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>There are at least 3 different ways to loop over arrays in JavaScript.  The three we will go over in this article are the<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>forEach<\/li>\n\n\n\n<li>for of<\/li>\n\n\n\n<li>for loop <\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Using forEach to loop through an array<\/h2>\n\n\n\n<p>The forEach takes a callback function that is executed for element of the array.  The callback function&#8217;s first argument is the element of the array.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">const myArray = [\"First\", \"Second\", \"Third\"]\n\nmyArray.forEach(function(myElement, index, array) {\n  console.log(`My Element is ${myElement}, index is ${index}, and array is {$array}`\n}<\/code><\/pre>\n\n\n\n<p>Couple things to note about forEach<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>You can not break out of the loop, it has to go through every element in the array.<\/li>\n\n\n\n<li>It is a higher order function<\/li>\n\n\n\n<li>The first argument is the array element, the second is the index, and the 3rd is the array itself<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Using for of to loop through an array<\/h2>\n\n\n\n<p>Using the for of loop, we can loop through the array with<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-dark-gray-background-color has-text-color has-background\"><code class=\"\">const myArray = [\"First\", \"Second\", \"Third\"]\n\nfor (const [i, myElement] of myArray) {\n  console.log(`My Element is ${myElement}, index is ${i}`)\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Looping through an array with a for loop<\/h2>\n\n\n\n<p>And using just a simple for loop, we can do<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">const myArray = [\"First\", \"Second\", \"Third\"]\n\nfor (let i = 0; i &lt; myArray.length; i++) {\n  console.log(`My Element is ${myarray[i]}, index is ${i}`)\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/50844095\/should-one-use-for-of-or-foreach-when-iterating-through-an-array\">https:\/\/stackoverflow.com\/questions\/50844095\/should-one-use-for-of-or-foreach-when-iterating-through-an-array<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are at least 3 different ways to loop over arrays in JavaScript. The three we will go over in this article are the Using forEach to loop through an array The forEach takes a callback function that is executed &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/3-different-ways-to-loop-through-arrays-in-javascript-in-2023\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1460,792],"tags":[430,1557,892,1437,1463,431,606],"class_list":["post-5430","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","tag-for","tag-for-of","tag-foreach","tag-javascript","tag-js","tag-loop","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5430"}],"version-history":[{"count":1,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5430\/revisions"}],"predecessor-version":[{"id":5431,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5430\/revisions\/5431"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}