JavaScript (ES2026) reference · Array Methods
JavaScript Array with() Method
Return a copy of the array with one element replaced, leaving the original unchanged. Negative indexes count from the end.
JavaScript (ES2026) Since ES2023
Syntax
array.with(index, value)
Parameters
| Parameter | Description |
|---|---|
index |
Position to replace; negative counts from the end. |
value |
The new value. |
Return value
A new array.
Examples
const days = ["Mon", "Tue", "Wed"];
console.log(days.with(1, "TUE"), days);
Output
[ 'Mon', 'TUE', 'Wed' ] [ 'Mon', 'Tue', 'Wed' ]
console.log([1, 2, 3].with(-1, 99));
Output
[ 1, 2, 99 ]
Version history
| Version | Change |
|---|---|
| JavaScript ES2023 | Added |
Related
array.at()· Return the element at an index, where negative indexes count from the end: arr.at(-1) is the last element.array.toSpliced()· Return a new array with elements removed, replaced or inserted at a position, leaving the original unchanged.array.splice()· Remove, replace or insert elements at a position, changing the array in place. Returns the removed elements.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial