Example 1 — Default String Sort
Sort words alphabetically and keep the original array intact.
const fruits = ["banana", "apple", "cherry"];
const sorted = fruits.toSorted();
console.log(sorted);
console.log(fruits); How It Works
- Start with fruits. Order is banana, apple, cherry.
- Call toSorted() with no compare. Values sort as strings (UTF-16).
- sorted is alphabetical. New array: apple, banana, cherry.
- Original stays.
fruitsis still banana, apple, cherry.

