Skip to solution
easyPhone Screen

How does Array.prototype.toReversed() avoid the classic bug of accidentally mutating a shared array reference?

284 views
01

Understand the problem

Question presented to candidate: "Two variables point at the same array -- a classic aliasing setup. If I call .reverse() on one of them, what happens to the other? How does .toReversed() avoid that problem entirely?"

What a strong answer should cover:

  • Array.prototype.reverse() reverses the array in place and returns the SAME array reference it was called on -- so any other variable or object property that was aliased to that same array reference sees the reversal too, which is often an unintended side effect.
  • Array.prototype.toReversed(), added in ES2023, computes the reversed order into a brand-new array and leaves the original completely untouched -- the source array and its length, order, and identity are all unaffected.
  • The bug this solves is aliasing: two variables, or a variable and a value stored elsewhere like React state or a Redux store, can point at the exact same array object without that being obvious from the code -- mutating through one reference silently corrupts what the other reference sees.
  • toReversed() always allocates a new array, which is a real cost (O(n) memory and time) compared to reverse(), which is O(n) time but no extra allocation -- worth naming as the actual tradeoff, not a free lunch.
  • The naming convention -- a to-prefixed method paired with an in-place counterpart -- also applies to toSorted()/sort(), toSpliced()/splice(), and with()/index assignment, all from the same ES2023 proposal.

Clarifying questions expected:

  • "Is the concern here specifically about a shared reference, like two variables or a value that got passed into two places, or about immutability as a general programming style?"
  • "Does this need to run in an older environment where toReversed might not be available yet?"

Code / implementation expected: Yes -- a real, executed side-by-side comparison where a second variable is aliased to the same array, showing reverse() leaking the mutation through the alias while toReversed() leaves the alias untouched.

es2023trickyreal-world
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-methods interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — captured output, not il

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JStoReversed() leaves a shared alias untouched; reverse() mutates it in place (run directly)
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 31 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track