JavaScript for in loop

The JavaScript for...in loop is a control structure that allows you to iterate over the properties of an object. It is used to access the properties of an object one by one. The for...in loop consists of two parts: the for keyword and the in keyword.

Syntax of the JavaScript for...in loop:

1for (var property in object) { 2 // code to be executed 3}

In this example, the property variable is used to store the name of the current property being processed in each iteration of the loop. The object is the object whose properties you want to iterate over.

Object key value iteration

Here's an example of how to use the for...in loop to print the properties and values of an object:

1var person = { 2 name: "William Max", 3 age: 21, 4 occupation: "Developer" 5}; 6 7for (var key in person) { 8 console.log(key + ": " + person[key]); 9} 10 11// Output: 12// name: William Max 13// age: 21 14// occupation: Developer

the person object has three properties: name, age, and occupation. The for...in loop is used to iterate over the properties of the person object. The key variable is used to store the name of the current property being processed in each iteration of the loop. The value of the property can be accessed using the person[key] expression.

The JavaScript for...in loop is useful when you need to iterate over the properties of an object and perform actions.