Update Array at Index (Immutable)

easy
By FrontendAtlas Editorial · Updated Oct 22, 2026
Return a new array with one index updated, leaving the original array untouched. This is the core immutable update pattern used in reducers, React state updates, and UI list editing, and it prevents accidental mutation.

Arguments

  • arr: any[] — The input array.
  • index: number — The index to update.
  • value: any — The new value to write.

Returns

any[] — A new array with the update applied (or a copy if out of range).

Solution idea

This is an immutable update helper: clone the array, update one index, and return the new array. It mirrors reducer patterns used in UI state management. Returning a new array every time avoids accidental mutation and keeps equality checks predictable for change detection and memoization.

Examples
updateAt([1, 2, 3], 1, 9); // => [1, 9, 3]

updateAt([1, 2], 5, 9); // => [1, 2]

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

  • Taro: Datadog senior frontend engineer interview, 2025 · Taro, September 2025

    One senior account describes live coding that starts simple and adds parts step by step, centred on data and tables. Immutable row updates are a supporting drill for that shape, not the reported exercise.

Reported in a public Datadog interview account. Datadog frontend interview questions