The ES2023 change-by-copy methods — toSorted, toReversed, and with — each return a new array instead of editing the one you called them on. They are the non-mutating twins of sort(), reverse(), and index assignment (arr[i] = x), which all rewrite the original array in place. Reaching for a copy is safer in code that shares arrays across a UI or a store, where a hidden in-place edit shows up as a bug somewhere else. You can read the spec on MDN.
Implement the three as standalone functions that each take the array as the first argument, return a new array, and never mutate the input.
toSorted(arr, compareFn?) // a sorted copy of arr
toReversed(arr) // a reversed copy of arr
with(arr, index, value) // a copy of arr with the slot at index replaced by value
toSorted([3, 1, 2], (a, b) => a - b); // [1, 2, 3] (input still [3, 1, 2])
toSorted([10, 2, 1]); // [1, 10, 2] (default sort is by string!)
toReversed([1, 2, 3]); // [3, 2, 1]
with([1, 2, 3], 1, 9); // [1, 9, 3]
with([1, 2, 3], -1, 9); // [1, 2, 9] (-1 is the last slot)
with([1, 2, 3], 3, 9); // throws RangeError (index === length)
compareFn, toSorted compares elements as strings by code unit, so [10, 2, 1] sorts to [1, 10, 2]. A compareFn(a, b) overrides that.with treats a negative index as counting from the end; the real index is index + length, so -1 is the last slot.with throws a RangeError if the index is < 0 or >= length.Array.prototype.toSorted, toReversed, or with.