View on GitHub

notes

A collection of my TIL notes and commonplace book entries.

for-in vs for-of

const arr = [1, 1, 2, 3, 5];

const obj = {
  foo: 'bar',
  foo2: ['b', 'ba', 'bar']
};

for-in on arrays prints indices

for (let i in arr) {
  console.log(i);
}

Output:

0
1
2
3
4

for-of on arrays prints its elements

for (let i of arr) {
  console.log(i);
}

Output:

1
1
2
3
5

for-in on objects prints keys

for (let i in obj) {
  console.log(i);
}

Output:

foo
foo2

for-of on objects: EXCEPTION!

for (let i of obj) {
  console.log(i);
}

Exception:

for (let i of obj) {
  ^

TypeError: obj is not iterable