Thursday, November 13, 2025
HomeLanguagesJavascriptExplain the differences between for (..in) and for (..of) statement in JavaScript

Explain the differences between for (..in) and for (..of) statement in JavaScript

Often in a JavaScript script, we iterate over some objects of few built-in classes like Arrays, Dictionaries, Strings, Maps, etc. We iterate the objects using loops. JavaScript supports different kinds of loops:

  • for loop
  • for (..in) loop
  • for (..of) loop
  • while loop
  • do-while loop

In this article, we will be learning about the difference between for (..in) and for (..of) Loops.

for (..in) loop: The JavaScript for (..in) statement loops through the enumerable properties of an object. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor’s prototype.

  • Syntax
    for (variable in object)
      statement
  • Example




    <!DOCTYPE html>
    <html>
      
    <body>
        <p id="demo"></p>
        <script>
            let person = {
                firstName: "neveropen",
                lastName: "<br>A Computer Science Portal for Geeks ",
                rank: 43
            };
            let userId = "";
            let i;
            for (i in person) {
                userId += person[i];
            }
            document.getElementById("demo").innerHTML = userId;
        </script>
    </body>
      
    </html>

    
    
  • Output: As you can see the for (..in) loop iterate over only the properties or the values of the dictionary object.
    neveropen
    A Computer Science Portal for Geeks 43
  • for (..of) loop: This for (..of) statement lets you loop over the data structures that are iterable such as Arrays, Strings, Maps, Node Lists, and more. It calls a custom iteration hook with instructions to execute on the value of each property of the object.

    • Syntax
      for (variable of iterable) {
        statement
      }
      
    • Example:




      <!DOCTYPE html>
      <html>
        
      <body>
          <p id="demo"></p>
          <script>
              let text = [
                  "neveropen",
                  " A Computer Science Portal for Geeks ",
                  "43"
              ];
              let userId = "";
              let i;
              for (i of text) {
                  userId += i;
              }
              document.getElementById("demo").innerHTML = userId;
          </script>
      </body>
        
      </html>

      
      
    • Output: As you can see the for (..of) loop iterate over only the content of the Array object.
      neveropen A Computer Science Portal for Geeks 43
    RELATED ARTICLES

    Most Popular

    Dominic
    32399 POSTS0 COMMENTS
    Milvus
    95 POSTS0 COMMENTS
    Nango Kala
    6765 POSTS0 COMMENTS
    Nicole Veronica
    11916 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11983 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6889 POSTS0 COMMENTS
    Ted Musemwa
    7141 POSTS0 COMMENTS
    Thapelo Manthata
    6835 POSTS0 COMMENTS
    Umr Jansen
    6838 POSTS0 COMMENTS