Change Array by Copy S4
- Stage: Stage 4
- Status: Finished
- ECMAScript edition: ES2023
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal adds non-mutating array methods to Array.prototype and TypedArray.prototype, such as toReversed, toSorted, toSpliced, and with, which return a new copy with the change applied. It aims to support immutable-style programming and interoperability with tuples.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Note This proposal is 'finished' and has been merged into the ECMAScript spec. PR
Change Array by copy
Provides additional methods on Array.prototype and TypedArray.prototype to enable changes on the array by returning a new copy of it with the change.
Status
This proposal is currently at Stage 4.
Champions
- Robin Ricard (Bloomberg)
- Ashley Claymore (Bloomberg)
Reviewers
Overview
This proposal introduces the following function properties to Array.prototype:
Array.prototype.toReversed() -> ArrayArray.prototype.toSorted(compareFn) -> ArrayArray.prototype.toSpliced(start, deleteCount, ...items) -> ArrayArray.prototype.with(index, value) -> Array
All of those methods keep the target Array untouched and returns a copy of it with the change performed instead.
toReversed, toSorted, and with will also be added to TypedArrays:
TypedArray.prototype.toReversed() -> TypedArrayTypedArray.prototype.toSorted(compareFn) -> TypedArrayTypedArray.prototype.with(index, value) -> TypedArray
These methods will then be available on subclasses of TypedArray. i.e. the following:
Int8ArrayUint8ArrayUint8ClampedArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayFloat32ArrayFloat64ArrayBigInt64ArrayBigUint64Array
Example
Motivation
The Tuple.prototype introduces these functions as a way to deal with the immutable aspect of the Tuples in Record & Tuple. While Arrays are not immutable by nature, this style of programming can be beneficial to users dealing with frozen arrays for instance.
This proposal notably makes it easier to write code able to deal with Arrays and Tuples interchangeably.
Relationship with Record & Tuple
While this proposal is derived from Record & Tuple, it should progress independently.
If web compatibility prescribes it, property names defined in this proposal are going to be changed. Those changes should be reflected on Tuple.prototype.
Implementations
-
Firefox/SpiderMonkey, shipping unflagged since Firefox 115
-
Safari/JavaScriptCore, shipping unflagged since Safari Tech Preview 146
-
Chrome/V8, shipping unflagged since Chrome 110
-
Ladybird/LibJS, shipping unflagged
-
./polyfill.js (minimalist reference implementation)