← Back to JavaScript, page 1

javascript-0017 · Easy fresher

What is the difference between forEach, for...of, and for...in?

forEach is an array method that calls a function for every item and cannot be stopped early. for...of loops over the values of any iterable and supports break, continue, and await. for...in loops over the enumerable property names (as strings) of an object, including inherited ones, so it is for objects and not for arrays.

Full answer

All three visit items, but they are made for different jobs.

forEach(callback) belongs to arrays (and a few other collections). It calls the callback with the item, its index, and the array. It always visits every item, and break or continue do not exist inside it. A return only leaves the current callback call. It returns undefined, and it does not wait for async callbacks.

for (const x of iterable) works on anything iterable: arrays, strings, Sets, Maps, generators. x is the value. Because it is a real loop, break, continue, return, and await all behave normally.

for (const key in object) visits property names as strings. It includes inherited enumerable properties, and the order for integer-like keys is ascending, then other keys by creation order. On an array it gives indexes as strings, and it would also show extra properties added to the array or its prototype. Use it for plain objects only when needed, and add a check with Object.hasOwn(object, key) for own properties. Often Object.keys, Object.values, or Object.entries with for...of is cleaner.

A plain object is not iterable, so for (const x of {}) throws a TypeError.

javascript
const marks = [40, 75, 90];
marks.extra = 'not an item';

marks.forEach((mark, index) => {
  if (mark > 50) return; // only skips this callback call
  console.log('forEach low mark at', index, mark);
});

for (const mark of marks) {
  if (mark > 80) break;
  console.log('for...of value:', mark);
}

for (const key in marks) {
  console.log('for...in key:', key, typeof key);
}

const base = { inherited: true };
const student = Object.create(base);
student.name = 'Isha';
for (const key in student) {
  console.log('object key:', key, Object.hasOwn(student, key) ? '(own)' : '(inherited)');
}

for (const [key, value] of Object.entries(student)) {
  console.log('entries:', key, value);
}

try {
  for (const x of {}) console.log(x);
} catch (error) {
  console.log(error.name);
}

Run with Node.js v24.22.1 as js-17.js; two runs gave identical output. Output: forEach low mark at 0 40 for...of value: 40 for...of value: 75 for...in key: 0 string for...in key: 1 string for...in key: 2 string for...in key: extra string object key: name (own) object key: inherited (inherited) entries: name Isha TypeError

Key points

  • forEach cannot be stopped and ignores async callbacks.
  • for...of iterates values of any iterable and supports break, continue, and await.
  • for...in iterates enumerable property names, including inherited ones.
  • Do not use for...in on arrays.
  • Plain objects are not iterable; use Object.entries for for...of.

Common mistakes

  • Using for...in on an array and getting string indexes and extra properties.
  • Trying to break out of forEach, or using it with await and assuming it waits.
  • Using for...of directly on a plain object.
  • Forgetting that for...in also walks the prototype chain.

Follow-ups

  • How would you stop early when you use forEach? What would you choose instead?
  • What does for...in do if a property is added or deleted during the loop?
  • What makes an object iterable?

Tips

  • Match each loop to a use: values for for...of, keys for for...in, simple side effects for forEach.
  • Mention the async pitfall of forEach; interviewers like it.
  • Offer Object.entries with for...of as the clean replacement for for...in on objects.