Course contentsShow
← JavaScript
Lesson 1083 of 2,72325. Iterators and GeneratorsFree lesson

Iterators vs Array-likes

Understanding the difference between iterables and objects with a length property.

Iterators vs Array-likes

You'll learn the key difference between objects that can be iterated and objects that merely look like arrays.

The Core Distinction

JavaScript has two similar-but-different concepts that beginners often confuse:

Iterables are objects that implement the iterator protocol (they have a Symbol.iterator method). You can use them with for...of, the spread operator, and Array.from().

Array-likes are objects that have a length property and numbered indices (like {0: 'a', 1: 'b', length: 2}), but don't necessarily have Symbol.iterator. They look like arrays but don't behave like them.

Why It Matters

// Array-like but NOT iterable
const arrayLike = {
  0: 'first',
  1: 'second',
  length: 2
};

// This FAILS because no Symbol.iterator
// for (const item of arrayLike) { } // TypeError!

// But Array.from() works with array-likes
const arr = Array.from(arrayLike); // ['first', 'second']

Some built-in objects are array-like but not iterable (like older DOM collections in some browsers). Others are iterable but not array-like (like Map and Set — they have no length property or numeric indices).

Arrays are both iterable and array-like, which is why they're so flexible.

The Analogy

Think of it like this: having a length property is like having a table of contents that says "I have 5 things." Being iterable is like having page numbers that let you actually flip through those things. Some books have both, some only have one.

Key Takeaway: Iterables support for...of via Symbol.iterator; array-likes just have length and indices. They're independent features that sometimes overlap.