for
const dataSet = []; for(let i = 0; i < dataSet.length; i++) { // do something }
for...of
Statement loops over iterable values
of an object
.
Elements OF
a Collection (array)
/* Array OF elements */ const arr = ['a', 'b', 'c']; for (const element of arr) { console.log(element); // 'a', 'b', 'c' };
for...in
Statement loops over iterable keys
of an object
.
A key IN
an Object
.
/* Object IN key */ const object = { a: '1', b: '2', c: '3' }; for (const key in object) { console.log(key); // 'a', 'b', 'c' console.log(object[key]); // '1', '2', '3' };
for...in
vs for...of
of
= value
values
of an arrayin
= key
property names
of an objectconst list = [4, 5, 6]; for (let key in list) { console.log(key); // "0", "1", "2", } for (let value of list) { console.log(value); // "4", "5", "6" }