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

ParameterDescription
index Position to replace; negative counts from the end.
value The new value.

Return value

A new array.

Examples

Replace one element in a copyTry it yourself
const days = ["Mon", "Tue", "Wed"];
console.log(days.with(1, "TUE"), days);

Output

[ 'Mon', 'TUE', 'Wed' ] [ 'Mon', 'Tue', 'Wed' ]
Negative indexTry it yourself
console.log([1, 2, 3].with(-1, 99));

Output

[ 1, 2, 99 ]

Version history

VersionChange
JavaScript ES2023Added