{"id":926,"date":"2022-07-11T19:49:31","date_gmt":"2022-07-11T19:49:31","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=926"},"modified":"2022-07-11T19:49:33","modified_gmt":"2022-07-11T19:49:33","slug":"for-of-loop-javascript","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/for-of-loop-javascript\/","title":{"rendered":"For of Loop Javascript"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>For of Loop Javascript<\/strong><\/p>\n<p>Well, the for&#8230; of the loop is kind of interesting, and convenient to be used, when we want to iterate through iterable objects majorly, like arrays, strings, map, set (there is another loop called as for&#8230; in, which we are going to majorly use with objects(with non-symbol enumerable properties(if you did not get this, simply assume that we are going to iterate through enumerable properties of an object))).<\/p>\n<p>The syntax is also simple, and much more readable so that it also becomes easy for us, to go through the program.<\/p>\n<p>First of all, let&#8217;s have a look at how are we doing this with help of the regular for loop(about which, we already know) &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-927 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop.jpg\" alt=\"\" width=\"921\" height=\"194\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop.jpg 921w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-300x63.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-768x162.jpg 768w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><br \/>\nAs you can see, we got an array, and then we are running a regular for loop, to iterate through the array. If we try to have a look at the output, it comes out to be something like this &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-928 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-1.jpg\" alt=\"\" width=\"1198\" height=\"385\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-1.jpg 1198w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-1-300x96.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-1-1024x329.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-1-768x247.jpg 768w\" sizes=\"auto, (max-width: 1198px) 100vw, 1198px\" \/><\/p>\n<p>As you can see, we were able to iterate through the array using the regular for loop. But, now, let&#8217;s do this using the for&#8230; of the loop. First of all, let&#8217;s have a look at the syntax, and then we will jump to the example &#8211;<\/p>\n<p>for (variable of iterable){<br \/>\n\/\/ some statements&#8230;<br \/>\n}<\/p>\n<p>Here, the variable is like a container, which is going to hold a new value from the iterable, in each iteration. We can create the variable using the let, const, or var keyword. This thing is very simple, and even if you haven&#8217;t got this thing, don&#8217;t worry, because this is just the syntax, and we are going to have a look at the program.<\/p>\n<p>Now, let&#8217;s have a look at the example &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-929 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-2.jpg\" alt=\"\" width=\"934\" height=\"245\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-2.jpg 934w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-2-300x79.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-2-768x201.jpg 768w\" sizes=\"auto, (max-width: 934px) 100vw, 934px\" \/><br \/>\nAs you can see, how simple the syntax is, and also, we are able to iterate through each element from the array here. If we try to have a look at the output now, the output is something like this &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-930 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-3.jpg\" alt=\"\" width=\"1198\" height=\"400\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-3.jpg 1198w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-3-300x100.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-3-1024x342.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/04\/For-of-loop-3-768x256.jpg 768w\" sizes=\"auto, (max-width: 1198px) 100vw, 1198px\" \/>As you can see, we could not see any change from the previous output, where we had done the same thing with the regular for loop. This is simply amazing, and we can make use of the for&#8230; of loop as and when required. This syntax looks like much more readable, and easy to be understood and implemented. You can also try some examples, using the for&#8230; of loop, so as to get familiar with the concept.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For of Loop Javascript Well, the for&#8230; of the loop is kind of interesting, and convenient to be used, when we want to iterate through iterable objects majorly, like arrays, strings, map, set (there is another loop called as for&#8230; in, which we are going to majorly use with objects(with non-symbol enumerable properties(if you did [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":{"0":"post-926","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-javascript-tutorial","7":"tag-javascript","8":"tag-javascript-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=926"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"predecessor-version":[{"id":2214,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/926\/revisions\/2214"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}