For AI agents: the complete documentation index is available at /tc39-atlas/en/llms.txt, the full documentation bundle is available at /tc39-atlas/en/llms-full.txt, and this page is available as Markdown at /tc39-atlas/en/proposals/proposal-change-array-by-copy.md.
  • English
  • Change Array by Copy S4

    Proposal details
    Proposal overview

    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.

    Note

    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() -> Array
    • Array.prototype.toSorted(compareFn) -> Array
    • Array.prototype.toSpliced(start, deleteCount, ...items) -> Array
    • Array.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() -> TypedArray
    • TypedArray.prototype.toSorted(compareFn) -> TypedArray
    • TypedArray.prototype.with(index, value) -> TypedArray

    These methods will then be available on subclasses of TypedArray. i.e. the following:

    • Int8Array
    • Uint8Array
    • Uint8ClampedArray
    • Int16Array
    • Uint16Array
    • Int32Array
    • Uint32Array
    • Float32Array
    • Float64Array
    • BigInt64Array
    • BigUint64Array

    Example

    const sequence = [1, 2, 3];
    sequence.toReversed(); // => [3, 2, 1]
    sequence; // => [1, 2, 3]
    
    const outOfOrder = new Uint8Array([3, 1, 2]);
    outOfOrder.toSorted(); // => Uint8Array [1, 2, 3]
    outOfOrder; // => Uint8Array [3, 1, 2]
    
    const correctionNeeded = [1, 1, 3];
    correctionNeeded.with(1, 2); // => [1, 2, 3]
    correctionNeeded; // => [1, 1, 3]

    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