JavaScript · Middle · Question 53 of 75
How does arr.toSorted() differ from arr.sort()?
- ✓Correct answer:
toSortedreturns a new sorted array and leaves the original untouched;sortsorts in place - 2
toSortedsorts numbers correctly without a comparator - 3They are identical;
toSortedis an alias
Explanation
ES2023 added non-mutating copies: toSorted, toReversed, toSpliced and with(index, value). They are handy for React/Redux state where mutation is a bug.