Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array with() Method

JavaScript Array with Method

Array Object Reference JavaScript Array object

ES2023 introduced the Array with() method, a safe way to update array elements without modifying the original array.

Example

Modify the value of the second element of the array:

const

sites

=

[

"

Google

"

,

"

Baidu

"

,

"

Runoob

"

,

"

Taobao

"

]

;

const

site

=

sites

.

with

(

1

,

"

JYSHARE

"

)

;

site Output result:

Google,JYSHARE,Runoob,Taobao

Try it »


Definition and Usage

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

The with() method returns a new array.

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


Browser support

with() is a feature of ES2023.

Since July 2023, all modern browsers support this feature:

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

Syntax

array.with(index, value)

Parameters

Parameters Description
index Required. The index (position) of the element to change. Negative indices count from the end of the array.
value Required. The new value.

Return Value

Type Description
Array object Returns a new array containing the changed element.

Array Object Reference JavaScript Array object