JavaScript(JS) for...in Loop

In JavaScript, the for...in loop is used to iterate over the properties of an object. It allows you to loop through the keys of an object and perform an action on each property. Here's the basic syntax for a for...in loop:

r‮:ot refe‬theitroad.com
for (let property in object) {
  // do something with object[property]
}

In this syntax, property is a variable that holds the name of each property in the object, and object is the object that you want to loop through.

Here's an example that demonstrates how to use the for...in loop to iterate over the properties of an object:

let person = { name: 'John', age: 30, city: 'New York' };

for (let property in person) {
  console.log(property + ': ' + person[property]);
}

In this example, the for...in loop iterates over the properties of the person object and logs each property name and its value to the console. The output of this code would be:

name: John
age: 30
city: New York

Note that the for...in loop should only be used for iterating over object properties, not for iterating over the elements of an array. To iterate over the elements of an array, you should use a for loop or a forEach method.