JavaScript Array.prototype.with() Method
The with() method returns a copy of an array with one element replaced, leaving the original unchanged.
Description
The with() method is the copying way to change one element: it returns a new array identical to the original except at the given index. It's the non-mutating equivalent of arr[i] = value, and like at() it accepts negative indexes.
It's part of the "change array by copy" group added in 2023, alongside toReversed(), toSorted() and toSpliced(). Unlike bracket assignment, it can't add new elements: an index outside the array throws a RangeError instead of extending it.
Because it returns a new array, calls can be chained, as in arr.with(0, 'a').with(1, 'b'), and it fits immutable update patterns in UI frameworks, where replacing an item in a list should create a new list. The copy is shallow, and empty slots become undefined.
Syntax
array.with(index, value)Parameters
- indexThe index to change. Negative values count from the end.
- valueThe new value.
Return Value
A new array with the element at index replaced.
Exceptions
- RangeErrorThrown if the index is outside the array.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.with() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





