JavaScript Array with()


JavaScript Array with() Method

ES2023 added the Array with() method as a safe way to update elements in an array without altering the original array.

Example

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

Description

The with() method updates a specified array element.

The with() method returns a new array.

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


Syntax

array.with(index, value)

Parameters

Parameter Description
index Required.
The index (position) of the item to change.
A negative index counts from the end of the array.
value Required.
The new value.

Return Value

A new array containing the changed element.

Browser Support

with() is an ES2023 feature.

It is supported in all modern browsers since July 2023:

Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
Feb 2023 Feb 2023 Jul 2023 Mar 2023 May 2023


Previous JavaScript Array Reference Next
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.