JavaScript Loops


for x in y

Used for iterating over objects

const object = { a: 1, b: 2, c: 3 };
 
for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}
 
// "a: 1"
// "b: 2"
// "c: 3"

for x of y

Used for strings, arrays, array-like objects

const array1 = ['a', 'b', 'c'];
 
for (const element of array1) {
  console.log(element);
}
 
// "a"
// "b"
// "c"