Skip to the solution
MediumFrontend7 min read

What do Array.from and Array.of do?

134 views

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 answered β€” new 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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 114 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is a pure function?

Continue