Question presented to candidate: "How does Array.prototype.with() work, what does it return compared to something like arr[index] = value or splice, and when would you actually reach for it in real code?"
What a strong answer should cover:
- with(index, value) returns a brand-new array with the element at index replaced by value; the original array is left completely unchanged, both its reference and its contents.
- This contrasts with direct index assignment (arr[i] = value) and splice(), both of which mutate the original array in place and return either the removed elements (splice) or nothing meaningful.
- with() supports negative indices, counting back from the end, the same way at() does.
- An out-of-range index, positive or negative, throws a RangeError rather than silently no-op-ing or growing the array.
- Real use case: state-management code (Redux reducers, React state updates) that must return a new reference so change detection works correctly -- with() replaces the "spread then mutate a nested array" pattern, which is a common source of accidental mutation bugs.
Clarifying questions expected:
- "Is this specifically about the ES2023 change-array-by-copy methods, or should I compare it against the full set of array mutation methods too?"
- "Should I talk through a concrete state-management bug this method avoids?"
Code / implementation expected: Yes -- a runnable comparison of with() against splice()-based mutation and a copy-then-assign approach, plus a demonstrated state-management bug it avoids.