← Back to JavaScript, page 4

javascript-0066 · Easy fresher

What is an array-like object, and how is it different from an iterable and a real array?

An array-like object has a length and indexed items but is not an Array, so it lacks methods such as map and filter. An iterable has a Symbol.iterator method and works with for...of and spread. Array.from converts either kind into a real array and accepts a mapping function, which also makes it a tool for generating sequences.

Full answer

Three related ideas are easy to confuse.

A real array is created by [], Array.of, or Array.from. Array.isArray returns true for it, and it has all array methods. An array-like object has numeric keys starting at 0 and a length property, for example the arguments object, a DOM NodeList, an HTMLCollection, or the plain object { 0: 'a', 1: 'b', length: 2 }. It can be read with a for loop and an index, but calling map on it fails because the method does not exist.

An iterable is anything with a [Symbol.iterator]() method: arrays, strings, Sets, Maps, generators, and many DOM lists. for...of, spread, and destructuring work with iterables. An object can be array-like, iterable, both, or neither. A string is both. The plain object above is array-like but not iterable, so [...obj] throws a TypeError, while a Set is iterable but has no length or indexes.

Array.from(source, mapFn) accepts either kind and returns a new real array. Its second argument maps each item as the array is built. A useful trick is Array.from({ length: 5 }, (_, i) => i * 2), which creates a sequence without holes. By contrast, new Array(5) creates a sparse array with empty slots that map skips, and fill can set them.

Array methods can also be borrowed with Array.prototype.map.call(arrayLike, fn), though Array.from is the modern choice.

javascript
function showArguments() {
  return [Array.isArray(arguments), arguments.length, typeof arguments.map];
}
console.log(showArguments(1, 2, 3));

const arrayLike = { 0: 'a', 1: 'b', length: 2 };
console.log(Array.from(arrayLike), Array.prototype.map.call(arrayLike, (x) => x + x));
try {
  console.log([...arrayLike]);
} catch (error) {
  console.log('spread of array-like:', error.name);
}

const set = new Set(['x', 'y']);
console.log(typeof set.length, [...set], Array.from(set, (v) => v.toUpperCase()));
console.log(Array.from('héllo'), Array.from(new Map([['k', 1]])));

console.log(Array.from({ length: 5 }, (_, i) => i * 2));
console.log(Array.from({ length: 3 }, () => 'row'));

const sparse = new Array(3);
console.log(sparse, sparse.map(() => 1), 0 in sparse);
console.log(new Array(3).fill(0), [...new Array(3)].map((_, i) => i));

const grid = Array.from({ length: 2 }, () => Array(2).fill('.'));
grid[0][0] = '#';
console.log(grid);
const brokenGrid = new Array(2).fill(Array(2).fill('.'));
brokenGrid[0][0] = '#';
console.log(brokenGrid);

console.log(Array.of(3), Array(3).length, Array.isArray(Array.from(arrayLike)));

Run with Node.js v24.22.1 as js-66.js; two runs gave identical output. Output: [ false, 3, 'undefined' ] [ 'a', 'b' ] [ 'aa', 'bb' ] spread of array-like: TypeError undefined [ 'x', 'y' ] [ 'X', 'Y' ] [ 'h', 'é', 'l', 'l', 'o' ] [ [ 'k', 1 ] ] [ 0, 2, 4, 6, 8 ] [ 'row', 'row', 'row' ] [ <3 empty items> ] [ <3 empty items> ] false [ 0, 0, 0 ] [ 0, 1, 2 ] [ [ '#', '.' ], [ '.', '.' ] ] [ [ '#', '.' ], [ '#', '.' ] ] [ 3 ] 3 true

Key points

  • Array-like: numeric indexes plus length, but no array methods.
  • Iterable: has Symbol.iterator and works with for...of and spread.
  • An object can be one, both, or neither; a string is both.
  • Array.from converts either kind and takes an optional map function.
  • new Array(n) creates empty slots; Array.from({ length: n }, fn) creates real items.

Common mistakes

  • Calling map or filter directly on a NodeList or arguments object.
  • Using new Array(n).map(...) and getting skipped empty slots.
  • Filling a two-dimensional grid with one shared inner array through fill.
  • Spreading an array-like object that is not iterable.

Follow-ups

  • Which DOM collections are iterable and which are only array-like?
  • What does Array(3) differ from Array.of(3)?
  • How do typed arrays relate to normal arrays?

Tips

  • Define the three terms in one sentence each, then show the object that is array-like but not iterable.
  • Mention the Array.from({ length }) idiom; it is useful in many coding questions.
  • Point out the fill trap with nested arrays.