Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2023.
The with() method of Array instances is the copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value.
Zero-based index at which to change the array, converted to an integer.
Any value to be assigned to the given index.
A new array with the element at index replaced with value.
Thrown if index >= array.length or index < -array.length.
The with() method changes the value of a given index in the array, returning a new array with the element at the given index replaced with the given value. The original array is not modified. This allows you to chain array methods while doing manipulations.
By combining with() with at(), you can both write and read (respectively) an array using negative indices.
The with() method never produces a sparse array. If the source array is sparse, the empty slots will be replaced with undefined in the new array.
The with() method is generic. It only expects the this value to have a length property and integer-keyed properties.
With the with() method, you can update a single element in an array and then apply other array methods.
The with() method always creates a dense array.
The with() method creates and returns a new array. It reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. As each property of this is accessed, the array element having an index equal to the key of the property is set to the value of the property. Finally, the array value at index is set to value.
| ECMAScript® 2027 Language Specification # sec-array.prototype.with |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.