JavaScript 2025 · Arrays

Sort numbers without changing the original

Sort ascending. sort() compares as text by default and changes the array, so toSorted with a comparator is safer.

const sorted = numbers.toSorted((a, b) => a - b);

Official JavaScript 2025 documentation

More arrays commands

All arrays commands · JavaScript 2025 cheatsheet

Was this page helpful?