for...in loop

suggest change

Warning: for...in iterates enumerable string keys, including inherited ones. It is intended for object properties, not array values. Prefer for...of for arrays and other iterables. Non-enumerable and symbol-keyed properties are not visited.

const object = { a: 'foo', b: 'bar', c: 'baz' };

Object.defineProperty(object, 'a', {
  enumerable: false,
});

for (const key in object) {
  if (Object.hasOwn(object, key)) {
    console.log(`object.${key}, ${object[key]}`);
  }
}

Expected output:

object.b, bar
object.c, baz

When inherited properties are not wanted, Object.keys(), Object.values(), or Object.entries() is usually simpler because those methods return only own enumerable properties.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents