Programming Search⌘K

Featurecore

Array-like objects

Also called array-like, array-like object, arguments object, NodeList, generic array methods, Array.prototype.call.

An array-like object has a length and indexed elements but is not an Array, like arguments, a NodeList or a string. Array methods run on it through call, and Array.from turns it into an array.

Language
JavaScript

Synopsis

const like = { 0: "a", 1: "b", 2: "c", length: 3 };

console.log(Array.isArray(like), typeof like.map);
console.log(Array.prototype.join.call(like, "+"));
console.log(Array.prototype.map.call(like, (s) => s.toUpperCase()));
console.log(Array.from(like), Array.from("hey"));

function count() {
  return [arguments.length, Array.isArray(arguments), [...arguments]];
}
console.log(count(10, 20));
Output
false undefined
a+b+c
[ 'A', 'B', 'C' ]
[ 'a', 'b', 'c' ] [ 'h', 'e', 'y' ]
[ 2, false, [ 10, 20 ] ]
Task Code
run an array method on it Array.prototype.join.call(obj, ",")
make a real array Array.from(obj)
make a real array from an iterable one [...obj]
make a real array and map it Array.from(obj, fn)

Semantics

r1. An array-like object "is expected to actually have a length property and to have indexed elements in the range 0 to length - 1".1 MDN defines the term more loosely as "any object that doesn't throw during the length conversion process".1 The arguments object, NodeList and HTMLCollection are array-like.1

r2. An array-like object is not an Array. It may "look and behave like arrays on the surface but do not share all of their methods."2 So like.map is undefined, and "Array methods cannot be called directly on array-like objects."2

r3. "Array methods are always generic — they don't access any internal data of the array object. They only access the array elements through the length property and the indexed elements."1 So Array.prototype.method.call(obj, ...) runs the method on obj.2 The specification says of these methods that they "are not dependent upon their this value being an Array exotic object."3 Example: like.

r4. The method converts length first. "The length property is converted to an integer and then clamped to the range between 0 and 253 - 1", that is, 2 to the power 53, minus 1. A missing length "behaves as if it has value 0".1 An index below 0 or above length - 1 is ignored.1 A missing index in the range behaves as an empty slot of a Sparse arrays array.1 Example: length.

r5. Array.from turns an array-like object into an Array: it works on an iterable, "or, if the object is not iterable, array-like objects (objects with a length property and indexed elements)."4 A missing index becomes undefined.4 For arguments, MDN lists three ways: "slice() , Array.from() , or spread syntax."5 Spread is narrower: "Only iterable values, like Array and String , can be spread in array literals and argument lists."6

r6. Strings are array-like, and "Array prototype methods can be used on strings as well".2 A method that writes does not work on a string, "as strings are immutable."7 Example: string.

Examples

How the methods read length (rule r4).

const sparse = { 0: "x", 2: "z", 5: "ignored", length: 3 };
console.log(Array.prototype.join.call(sparse, "-"));
console.log(Array.from(sparse));

console.log(Array.prototype.map.call({}, (x) => x));
console.log(Array.prototype.join.call({ 0: "a", 1: "b", length: "2" }));
Output
x--z
[ 'x', undefined, 'z' ]
[]
a,b

Index 5 is past the length, so the methods do not see it. An object with no length is empty, and the string '2' converts to the number 2.

Read-only methods work on strings; writing ones do not (rule r6).

const word = "stressed";
console.log(Array.prototype.map.call(word, (c) => c.charCodeAt(0)).slice(0, 3));
console.log(Array.from(word).reverse().join(""));
try {
  Array.prototype.push.call(word, "!");
} catch (e) {
  console.log(e.name);
}
Output
[ 115, 116, 114 ]
desserts
TypeError

Pitfalls

Calling an array method directly

The guide shows the error: arguments.forEach "is not a function".2

This code runs but gives a wrong answer on purpose.

function printAll() {
  try {
    arguments.forEach((x) => console.log(x));
  } catch (e) {
    console.log(e.name);
  }
  Array.from(arguments).forEach((x) => console.log(x));
}
printAll("a", "b");
Output
TypeError
a
b

Convert the object with Array.from first, or use a rest parameter, (...items): "The arguments object is not a real array, while rest parameters are Array instances".8

Spread on a plain array-like object

[...obj] needs an iterable, and "Many objects are not iterable, including all plain objects that lack a Symbol.iterator method".6 { 0: "a", length: 1 } is array-like but not iterable, so spread throws. Array.from works on both kinds (rule r5).

This code runs but gives a wrong answer on purpose.

const obj = { 0: "a", length: 1 };
try {
  console.log([...obj]);
} catch (e) {
  console.log(e.name);
}
console.log(Array.from(obj));
Output
TypeError
[ 'a' ]

Comparison

Python's sequence is the closest idea. It is "An iterable which supports efficient element access using integer indices via the getitem() special method and defines a len() method".9 Java uses an interface: a List is "An ordered collection", and "The user can access elements by their integer index".10 In Go, a string has indexed bytes: "A string's bytes can be accessed by integer indices 0 through len(s)-1".11 In JavaScript, any object with a length works with the generic array methods (rule r3).

References

  1. MDN Web Docs, JavaScript Reference, Array, Generic array methods; Normalization of the length property; Array-like objects
  2. MDN Web Docs, JavaScript Guide, Indexed collections, Working with array-like objects
  3. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), 23.1.3 Properties of the Array Prototype Object
  4. MDN Web Docs, JavaScript Reference, Array.from(), Description
  5. MDN Web Docs, JavaScript Reference, The arguments object, the introduction; Description
  6. MDN Web Docs, JavaScript Reference, Spread syntax (...), Description
  7. MDN Web Docs, JavaScript Reference, Array.prototype.fill(), Description
  8. MDN Web Docs, JavaScript Reference, Rest parameters, The difference between rest parameters and the arguments object
  9. The Python Glossary, Python 3.14, sequence
  10. Java SE 25 API, Interface List, the interface description
  11. The Go Programming Language Specification, String types

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source