<?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: Michael Bowlin</title>
    <description>The latest articles on DEV Community by Michael Bowlin (@jaidoutani).</description>
    <link>https://dev.to/jaidoutani</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%2F802692%2F0f1dd182-928c-40b8-ac7d-a07a734fc69f.jpg</url>
      <title>DEV Community: Michael Bowlin</title>
      <link>https://dev.to/jaidoutani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaidoutani"/>
    <language>en</language>
    <item>
      <title>For...of? Or for...in? Which to choose!</title>
      <dc:creator>Michael Bowlin</dc:creator>
      <pubDate>Sat, 14 May 2022 23:06:53 +0000</pubDate>
      <link>https://dev.to/jaidoutani/forof-or-forin-which-to-choose-8l2</link>
      <guid>https://dev.to/jaidoutani/forof-or-forin-which-to-choose-8l2</guid>
      <description>&lt;p&gt;Need to iterate through some properties, but not sure which statement to choose? I got you. &lt;/p&gt;

&lt;p&gt;According to our handy-dandy MDN, "the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/a&gt; statement creates a loop iterating over iterable objects." Iterable objects are those that have a default behavior when iterating such as Array or Map. &lt;br&gt;
The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#why_use_for...in"&gt;&lt;code&gt;for...in&lt;/code&gt;&lt;/a&gt; statement iterates over all enumerable properties of an object that are keyed by strings." Enumerable properties are properties that are set to true by default. So no symbol properties will work with this loop! Because it iterates over all of the properties, it may be helpful to use &lt;code&gt;for...in&lt;/code&gt; when debugging. &lt;/p&gt;

&lt;p&gt;Take a look at the examples below.&lt;/p&gt;

&lt;h2&gt;
  
  
  for...of:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const superHeroes = ["Super Man", "Batman", "Hawkgirl", "Wonder Woman"]

for(const superHero of superHeroes) {
  console.log(superHero)
}
// expected output: Super Man
// expected output: Batman
// expected output: Hawkgirl
// expected output: Wonder Woman
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example above, superHeroes is our array of objects. &lt;code&gt;for...of&lt;/code&gt; returns the values. In this case, they are Super Man, Batman, Hawkgirl, and Wonder Woman while the keys are the indexes of each — 0, 1, 2, 3. If you're unsure which index belongs to which property, you can always use indexOf method or simply change the &lt;code&gt;for...of&lt;/code&gt; to a &lt;code&gt;for...in&lt;/code&gt; loop to see the keys. &lt;/p&gt;

&lt;h2&gt;
  
  
  for...in:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const heroIdentities = {
  "Clark Kent": "Super Man",
  "Bruce Wayne": "Batman",
  "Shayera Hol": "Hawkgirl",
  "Diana": "Wonder Woman"
}
for(const identity in heroIdentities) {
  console.log(`${identity} is: ${heroIdentities[identity]}`)
}
// expected output: Clark Kent is: Super Man
// expected output: Bruce Wayne is: Batman
// expected output: Shayera Hol is: Hawkgirl
// expected output: Diana is: Wonder Woman
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;for...in&lt;/code&gt; example, you have an object, heroIndentities, which has key/value pairs in it. The keys are the names of the superheroes: Bruce Wayne, Clark Kent, etc. The values are their aliases: Super Man, Batman, etc. &lt;code&gt;for...in&lt;/code&gt; iterates and returns the keys. The reason my examples output returns both the key and the value is because I used bracket notation —&lt;code&gt;heroIdentities[identity]&lt;/code&gt;— to access the objects properties.&lt;/p&gt;

&lt;p&gt;Rule of thumb: &lt;code&gt;for...of&lt;/code&gt; is typically used for arrays or array-like objects and &lt;code&gt;for...in&lt;/code&gt; is typically used for objects. &lt;/p&gt;

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