10. for…of vs for…in

  • for...of iterates over iterable objects, such as array.
  1. const nicknames = ['di', 'boo', 'punkeye'];
  2. nicknames.size = 3;
  3. for (let nickname of nicknames) {
  4. console.log(nickname);
  5. }
  6. // di
  7. // boo
  8. // punkeye
  • for...in iterates over all enumerable properties of an object.
  1. const nicknames = ['di', 'boo', 'punkeye'];
  2. nicknames.size = 3;
  3. for (let nickname in nicknames) {
  4. console.log(nickname);
  5. }
  6. // 0
  7. // 1
  8. // 2
  9. // size