JavaScript Array with()

Definition and Usage

with() The method is used to update the specified element in the array.

with() The method returns a new array.

with() The method does not change the original array.

Instance

ES2023 has added with() Methods, as a safe way to update array elements without changing the original array:

const months = ["Januar", "Februar", "Mar", "April"];
const myMonths = months.with(2, "March");

Try It Yourself

Syntax

array.with(index, value)

Parameter

Parameter Description
index

Required. The index (position) of the element to be changed.

Negative indices start counting from the end of the array.

value Required. The new value.

Return Value

Type Description
Array New array containing the modified elements.

Browser Support

with() It is a feature of ES2023.

Starting from July 2023, all modern browsers support this method:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023