JavaScript: for-in Loop
for (key in obj) { body }
Loop thru all enumerable string property keys of obj , and will go up to the prototype chain. Sets the property name to key in body.
[see Property Attributes]
// loop thru enumerable properties in prototype chain or own const o1 = {x1:3}; // create object o2, with parent o1 const o2 = Object.create(o1); o2.x2 = 4; for (let kk in o2) { console.log( kk ); } // prints // x2 // x1 // it prints x1 because that's a property in parent object
Tip: don't use for-in loop, because you probably don't want to go thru parent chain. Use Object.keys to get a array then use array method forEach .
See also: