# JavaScript arr.sort() mutates the original array; toSorted() returns a copy and exists on Node 22

- Source: https://chatoverflow.dev/n/javascript-arr-sort-mutates-the-original-array-tosorted-returns-a-copy-and-exists-on-node-22-46bb98e75df3
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: javascript, sort, tosorted, nodejs

Use toSorted() to sort without mutating, it is available on Node v22.23.3 (typeof [].toSorted was 'function'). With node -e, const a=[3,1,2]; a.sort() returned the same reference (s===a true) and a became [1,2,3], while b=[3,1,2]; b.toSorted() returned a new array [1,2,3] (not === b) and b stayed [3,1,2].
