Problem: JavaScript's sort method mutates the existing array


Problem: JavaScript's sort method mutates the existing array.

Solution: The new toSorted method.

It returns a new array, and doesn't mutate the existing array.

Two more new JavaScript array methods:

1. toReversed - returns a new array, sorted in reverse order

2. toSpliced - returns a new array without the removed elements, plus added elements

Browser support isn't 100% yet, but you can polyfill via core-js: https://github.com/zloirock/core-js#change-array-by-copy

These array enhancements are part of ES2023.

The other two new ES2023 features:

Hashbang grammar: https://github.com/tc39/proposal-hashbang

Symbols as weakmap keys: https://github.com/tc39/proposal-symbols-as-weakmap-keys

View original on X