<?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>Forem: NinadPatil</title>
    <description>The latest articles on Forem by NinadPatil (@ninadpatil).</description>
    <link>https://forem.com/ninadpatil</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%2F1370627%2Fe2162b7d-3370-4451-b103-e736955a71cc.jpeg</url>
      <title>Forem: NinadPatil</title>
      <link>https://forem.com/ninadpatil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ninadpatil"/>
    <language>en</language>
    <item>
      <title>Understanding the Difference Between Iterable and Enumerable Properties in JavaScript</title>
      <dc:creator>NinadPatil</dc:creator>
      <pubDate>Mon, 29 Jul 2024 13:02:08 +0000</pubDate>
      <link>https://forem.com/ninadpatil/understanding-the-difference-between-iterable-and-enumerable-properties-in-javascript-6oe</link>
      <guid>https://forem.com/ninadpatil/understanding-the-difference-between-iterable-and-enumerable-properties-in-javascript-6oe</guid>
      <description>&lt;p&gt;When working with JavaScript, you'll often need to loop through collections of data. Two common methods for doing this are the for-in and for-of loops. However, these loops are used in different contexts and for different types of collections. Understanding the distinction between iterable and enumerable properties is crucial for effectively using these loops.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The for-in Loop: Loops Through All Enumerable Properties
&lt;/h2&gt;

&lt;p&gt;The for-in loop is designed to iterate over the enumerable properties of an object. Enumerable properties are those that are visible when looping through an object or calling methods like Object.keys().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
    name: 'john',
    age: '24'
};

obj.isMarried = false;

for (const key in obj) {
    console.log(key); // Outputs: name, age, isMarried
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the for-in loop iterates over all enumerable properties of the obj object, including the dynamically added isMarried property.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The for-of Loop: Loops Through All Iterables
&lt;/h2&gt;

&lt;p&gt;The for-of loop, on the other hand, is used to iterate over iterable objects. An iterable is an object that has a Symbol.iterator method. Common examples of iterable objects include arrays, strings, maps, and sets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = ['apple', 'pear'];
arr.anotherFruit = 'banana';

for (const fruit of arr) {
    console.log(fruit); // Outputs: apple, pear
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the for-of loop iterates over the array arr, ignoring the anotherFruit property because it is not part of the iterable sequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Arrays Are Iterable, Objects Are Not Iterable
&lt;/h2&gt;

&lt;p&gt;By default, arrays in JavaScript are iterable because they have a built-in Symbol.iterator method. Plain objects, however, do not have this method and are therefore not iterable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = ['apple', 'pear'];

for (const fruit of arr) {
    console.log(fruit); // Outputs: apple, pear
}

const obj = {
    name: 'john',
    age: '24'
};

for (const key of obj) {
    console.log(key); // Throws TypeError: obj is not iterable
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, attempting to use a for-of loop on an object results in a TypeError because objects do not implement the Symbol.iterator method.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Error Explanation: TypeError: obj is not iterable
&lt;/h2&gt;

&lt;p&gt;When you try to use a for-of loop on a non-iterable object, JavaScript throws a TypeError. This error occurs because the object does not have the Symbol.iterator method, which is required for for-of loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
    name: 'john',
    age: '24'
};

try {
    for (const key of obj) {
        console.log(key);
    }
} catch (e) {
    console.error(e); // Outputs: TypeError: obj is not iterable
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;

&lt;p&gt;Below is a complete example that demonstrates the differences between the for-in and for-of loops:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const log = console.log;
const arr = ['apple', 'pear'];
arr.anotherFruit = 'banana';

log('Using for-of loop:');
for (const fruit of arr) {
    log(fruit); // Outputs: apple, pear
}

log('Using for-in loop:');
for (const fruit in arr) {
    log(fruit); // Outputs: 0, 1, anotherFruit
}

const obj = {
    name: 'john',
    age: '24'
};

obj.isMarried = false;

log('Using for-in loop:');
for (const key in obj) {
    log(key); // Outputs: name, age, isMarried
}

log('Using for-of loop:');
try {
    for (const key of obj) {
        log(key);
    }
} catch (e) {
    log(e); // Outputs: TypeError: obj is not iterable
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding the difference between iterable and enumerable properties is essential for effectively using JavaScript's for-in and for-of loops. The for-in loop is used for iterating over enumerable properties of objects, while the for-of loop is designed for iterating over iterable objects like arrays. Misusing these loops can lead to errors, such as the TypeError encountered when attempting to use a for-of loop on a non-iterable object. By grasping these distinctions, you can write more robust and error-free JavaScript code.&lt;/p&gt;

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