Favorites

JavaScript Array.prototype.with() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.with()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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

const colors = ['red', 'green', 'blue'];
console.log(colors.with(1, 'yellow'));
console.log(colors.with(-1, 'black'));
console.log(colors);

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee