for...in
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
// Expected output:
// "a: 1"
// "b: 2"
// "c: 3"
Description
The for...in loop is a loop in JavaScript that iterates over all enumerable string properties of an object.
Parameters
variable, object, statement
for (variable in object)
statement
Gotcha
Even though the for...in
works to loop over an array we should use a traditional for-loop, a forEach() or for...of
. As Arrays are just objects in JavaScript it would also loop over properties inherited from the prototype chain. It is also less performant.