JavaScript: for-in Loop

By Xah Lee. Date: . Last updated: .

for (key in obj) { body }

Loop thru property keys that has Property Attribute enumerable, and is a string key, and will go up to 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 .

// loop thru enumerable properties in prototype chain or own

const x1 = {k1:3};

// create object x2, with parent x1
const x2 = Object.create(x1);

x2.k2 = 4;

for (let kk in x2) { console.log( kk ); }

// prints
// k2
// k1

// it prints k1 because that's a property in parent object

JavaScript Property

JavaScript Loop, Iteration

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc