Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn JS For In Loop



The For In Loop


In JavaScript, the for...in loop is used to iterate over the properties of an object. It iterates through all enumerable properties of an object, including properties inherited from its prototype chain. Here's the basic syntax of the for...in loop:

        for (variable in object) {
          // code to be executed
      }
      

Here's an example of using the for...in loop to iterate over the properties of an object:

        const person = {
          name: "John",
          age: 30,
          profession: "Developer"
      };
      
      for (let key in person) {
          console.log(key + ": " + person[key]);
      } 
      

In this example:

The loop will iterate over each enumerable property of the person object and print both the property name and its corresponding value.

However, it's important to note that the for...in loop should not be used to iterate over arrays due to its behavior with array-like objects and potential issues with iterating over non-enumerable properties or inherited properties. Instead, for arrays, you should typically use a for loop or Array.forEach() method.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java