← TechnologyThe essentials of

with(): replacing one item in a copy

Publisher: ResumeMaker • 1 source

Published: 22 Oct 2026 13:13 · Updated: 22 Oct 2026 13:13

AI source check: 21 Oct 2026 12:40. This is not editorial approval.

In a few sentences

In brief

The index indicates which place to get the new value.

Useful to know

Array.prototype.with() returns a copy of the array with a replaced element of a specific index. For example, [4, 5, 6].with(1, 9) gives [4, 9, 6], with no change to the original. Negative indices . [1] are also allowed

The method is convenient when the state needs to be replaced with a new array. An index outside the scope leads to RangeError, so validate the position if it comes from a user's choice. It does not automatically add an arbitrary distant element and is not a substitute for an identifier search.

Sources

For further detail and to check the information:

  1. [1] MDN — Array.with ↗