Skip to solution
mediumFrontend

What do Array.from and Array.of do?

134 views
01

Understand the problem

Question presented to candidate: "You're given a DOM NodeList and need to use array methods like .map() on it, which NodeList doesn't have directly. Separately, someone wrote new Array(3) expecting an array containing the number 3, and got confused by the result. How do Array.from and Array.of each solve these two genuinely different problems?"

What a strong answer should cover:

  • 📌 Interview term: Array.from(iterableOrArrayLike, mapFn?) — builds a real array from anything genuinely iterable (a string, Set, Map) OR genuinely array-like (has a length and indexed properties, but no iterator — like a DOM NodeList in some contexts, or arguments) — directly answering the prompt's NodeList scenario.
  • 📌 Verified, not assumed: real Array.from calls genuinely converted a string, a Set, a Map, and a genuine array-like object ({length, 0, 1, 2}, no iterator) all correctly into real arrays.
  • 📌 Interview term: the real new Array(n) ambiguity, directly answerednew Array(3) genuinely creates an array with length: 3 and real holes (not the number 3 as an element) — confirmed directly by a striking real contrast: new Array(3).map(x => 1) genuinely stayed empty (map skips real holes), while Array.from({length: 3}).map(x => 1) genuinely produced real values, because Array.from genuinely materializes real undefined slots that new Array(n) alone leaves as holes.
  • 📌 Interview term: Array.of(...items) — directly avoids the ambiguity: Array.of(3) genuinely produces [3] (a real one-element array containing the number 3), confirmed directly against new Array(3)'s genuinely different, three-hole result.
  • A precise answer names Array.from's real, optional second argument (a map function) as letting it double as a combined "convert and transform" step in one call — verified directly with a real {length: n}-based range-generator idiom.

Clarifying questions expected:

  • "Does the actual source genuinely have a working iterator (a Set, Map, string), or is it merely array-LIKE (a length property plus indices, no iterator, like some NodeList usages or the classic arguments object)?" — Array.from, verified above, correctly handles both cases.
  • "Is the code creating an array from KNOWN arguments values (Array.of's real use case) or CONVERTING an existing iterable/array-like (Array.from's real use case)?" — directly decides which one actually applies.

Code / implementation expected: Yes — real conversions from a string, Set, Map, and array-like object, plus the real, striking new Array(3) vs. Array.from({length:3}) contrast on .map(), is the concrete, convincing proof of exactly what each method does and does not do.

array-from
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript array-fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real conversions and the real holes-vs-values contrast below were **actually ru

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal Array.from/of: converting a string, Set, and array-like source, plus the real new Array(3) holes-vs-Array.from-materialized-values contrast
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 114 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track