Free JavaScript reference · Python timeline

JavaScript ES2023: Array changes without mutating the original

Copying array methods and findLast help preserve input data and search from the end.

Edition 14 · Published · Editorial review October 26, 2026

Standardized in ES2023 · Built-in method

toSorted(), toReversed(), toSpliced(), and with()

The copying array methods return a new array while preserving the original sequence. They provide direct alternatives to mutating sort, reverse, splice, and indexed replacement.

The copy is shallow: nested objects are still shared. Sorting still needs an appropriate comparator; the default is string-based ordering rather than general numeric order. Array.with accepts a negative relative index, but throws for an out-of-range index.

The example sorts numerically and replaces the last element in a separate result. Neither operation edits values.

Worked example

Uses ES2023 features · Script

const values = [10, 2, 3];
console.log(JSON.stringify(values.toSorted((a, b) => a - b)));
console.log(JSON.stringify(values.with(-1, 9)));
console.log(JSON.stringify(values));

Expected output

[2,3,10]
[10,2,9]
[10,2,3]

Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.

Common mistake

A non-mutating method is not a deep clone. Forgetting the numeric comparator can still produce an incorrect order.

Try it yourself

Use toReversed and toSpliced on the same array, then confirm the original values are unchanged.

Read more: ES2023 specification · MDN explanation and compatibility

Standardized in ES2023 · Built-in method

findLast() and findLastIndex()

These methods scan from the end and return the final matching value or its index. They make last-match intent explicit without reversing the input array or translating an index back afterward.

findLast returns undefined when nothing matches. That result can also be a matching element's actual value, so findLastIndex may be better when the distinction matters. Its missing marker is -1; index zero is a successful match.

The predicate receives ordinary array-callback arguments. Keep side effects out of the predicate unless their reverse-order evaluation is intentional.

Worked example

Uses ES2023 features · Script

const values = [2, 5, 2];
console.log(values.findLastIndex(value => value === 2));
console.log(values.findLast(value => value > 3));
console.log(values.findLastIndex(value => value === 9));

Expected output

2
5
-1

Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.

Common mistake

Testing the returned index as a boolean treats zero as false and -1 as true. Compare with -1 explicitly.

Try it yourself

Use [2] and then [] to demonstrate both the index-zero success case and the missing case.

Read more: ES2023 specification · MDN explanation and compatibility

Recap

Copying array methods and findLast help preserve input data and search from the end. Check the engines your project supports before adopting new syntax or built-ins.

Read the ES2023 specification →