JS: for-in Loop

By Xah Lee. Date: . Last updated: .
for (key in obj) { body }
Loop thru property keys that are Enumerable Property and is a string key, including all such properties in the Prototype Chain.

Sets the property name to key in body.

πŸ’‘ TIP: don't use for-in loop, because you probably don't want to go thru parent chain. Use for-of Loop, or Object.keys and forEach .

const aa = { ak1: 1, ak2: 2 };
const bb = { bk1: 1, bk2: 2 };

// make aa to be parent of bb
Reflect.setPrototypeOf(bb, aa);

const xx = [];

// loop thru enumerable properties in prototype chain or own
for (let kk in bb) console.log(kk);

/*
prints

bk1
bk2
ak1
ak2
*/

JavaScript, Property

JavaScript, Loop, Iteration

BUY Ξ£JS JavaScript in Depth