What is the use of the for…in Loop in JavaScript ?

The for...in loop in JavaScript is used to iterate over the properties (keys) of an object. It allows you to loop through all enumerable properties of an object, including those inherited from its prototype chain.

Syntax:

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

Example: Here, the for...in loop iterates over the properties of the person object. For each iteration, the variable key takes on the name of a property and person[key] accesses the corresponding value.

Javascript




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


Output

name: John
age: 30
job: Developer

Contact Us