<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vidit Maniyar</title>
    <description>The latest articles on DEV Community by Vidit Maniyar (@viditmaniyar).</description>
    <link>https://dev.to/viditmaniyar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1069513%2F3cb55e7f-f02a-487e-9ecc-3c439f529689.jpeg</url>
      <title>DEV Community: Vidit Maniyar</title>
      <link>https://dev.to/viditmaniyar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/viditmaniyar"/>
    <language>en</language>
    <item>
      <title>Javascript for Noobs: The for...of and for...in Loops</title>
      <dc:creator>Vidit Maniyar</dc:creator>
      <pubDate>Sat, 22 Apr 2023 08:42:23 +0000</pubDate>
      <link>https://dev.to/viditmaniyar/javascript-for-noobs-the-forof-and-forin-loops-51m</link>
      <guid>https://dev.to/viditmaniyar/javascript-for-noobs-the-forof-and-forin-loops-51m</guid>
      <description>&lt;p&gt;In JavaScript, loops are an essential part of programming, and they help developers to iterate over data structures like arrays and objects. Two common types of loops in JavaScript are the &lt;code&gt;for of&lt;/code&gt; and &lt;code&gt;for in&lt;/code&gt; loops. Although these two loops look similar, they have different use cases and behave differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;for...of Loop&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for of&lt;/code&gt; loop was introduced in ECMAScript 6, and it is used to iterate over iterable objects like arrays and strings. The loop assigns each value of the iterable to a variable defined in the loop statement.&lt;/p&gt;

&lt;p&gt;Here is an example of a &lt;code&gt;for of&lt;/code&gt; loop iterating over an array of numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the loop iterates over the &lt;code&gt;numbers&lt;/code&gt; array and assigns each value to the &lt;code&gt;num&lt;/code&gt; variable. The loop then logs each number to the console.&lt;/p&gt;

&lt;p&gt;One important thing to note about the &lt;code&gt;for of&lt;/code&gt; loop is that it only works with iterable objects. It cannot be used to iterate over plain JavaScript objects or other non-iterable data types.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;for...in Loop&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for in&lt;/code&gt; loop is used to iterate over the keys of an object. Unlike the &lt;code&gt;for of&lt;/code&gt; loop, it can be used with any JavaScript object, including non-iterable objects.&lt;/p&gt;

&lt;p&gt;Here is an example of a &lt;code&gt;for in&lt;/code&gt; loop iterating over an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New York&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;John&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;New&lt;/span&gt; &lt;span class="nx"&gt;York&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the loop iterates over the &lt;code&gt;person&lt;/code&gt; object and logs each key and value to the console.&lt;/p&gt;

&lt;p&gt;It's important to note that the &lt;code&gt;for in&lt;/code&gt; loop doesn't guarantee the order of the keys. If the order of the keys is important, it's recommended to use another method like &lt;code&gt;Object.keys()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Usage and Best Practices&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now that we've looked at the differences between the &lt;code&gt;for of&lt;/code&gt; and &lt;code&gt;for in&lt;/code&gt; loops, let's summarize their recommended usage and best practices.&lt;/p&gt;

&lt;p&gt;Use the &lt;code&gt;for of&lt;/code&gt; loop when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Iterating over arrays and strings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need to access the values of the iterable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use the &lt;code&gt;for in&lt;/code&gt; loop when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Iterating over the keys of an object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need to access the properties of the object.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When using the &lt;code&gt;for in&lt;/code&gt; loop, it's important to keep in mind that it can iterate over inherited properties as well. To avoid this, use the &lt;code&gt;hasOwnProperty()&lt;/code&gt; method to check if the property belongs to the object itself or is inherited from its prototype chain. Conversely when you are adding a property to an object by using &lt;code&gt;Object.defineProperty()&lt;/code&gt; method, you can set the &lt;code&gt;enumerable&lt;/code&gt; to be &lt;code&gt;true&lt;/code&gt; explicitly to make it enumerable when using &lt;code&gt;for in&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In summary, the &lt;code&gt;for of&lt;/code&gt; and &lt;code&gt;for in&lt;/code&gt; loops are both important tools in JavaScript, but they have different use cases and behave differently. Understanding the differences between these two loops and when to use them will help you write more efficient and effective code.&lt;/p&gt;

&lt;p&gt;Fin.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Connect!
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/viditmaniyar/"&gt;https://www.linkedin.com/in/viditmaniyar/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Twitter: &lt;a href="https://twitter.com/viditmaniyar"&gt;https://twitter.com/viditmaniyar&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instagram: &lt;a href="https://www.instagram.com/viditmaniyar/"&gt;https://www.instagram.com/viditmaniyar/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Further Reading
&lt;/h3&gt;

&lt;p&gt;If you'd like to understand these topics in more depth, check out the following resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://exploringjs.com/es6/ch_for-of.html"&gt;Exploring ES6 eBook by Dr. Axel Rauschmayer&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.javascripttutorial.net/javascript-for-in/"&gt;Javascript for...in Loop on javascripttutorial.net&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.javascripttutorial.net/es6/javascript-for-of/"&gt;Javascript for...of Loop on javascripttutorial.net&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>es6</category>
    </item>
  </channel>
</rss>
