JS: for-in Loop ❌

By Xah Lee. Date: . Last updated: .
for (key in obj) { body }

Sets the property name to key in body.

🟢 TIP: for-in loop is almost never useful, because you don't want to go thru parent chain. Use for-of Loop, or Object.keys and forEach .

const xdad = { car1: 1, car2: 2 };
const xson = { toy1: 1, toy2: 2 };

// make xdad to be the parent of xson.
Reflect.setPrototypeOf(xson, xdad);

// for-in loop on xson
for (let kk in xson) console.log(kk);

// toy1
// toy2
// car1
// car2

// result also show keys from parent

JavaScript. Property

JavaScript. Loop, Iteration

JavaScript. Access Properties