How to sort array and get unique entries with JavaScript?

To sort array and get unique entries with JavaScript, we use a set.

For instance, we write

const myData = ["237", "124", "255", "124", "366", "255"];
const uniqueAndSorted = [...new Set(myData)].sort();

to convert the myData array to a set with the Set constructor to remove the duplicates.

Then we spread the set elements into an array with the spread operator.

Finally, we sort the array without the duplicates with sort.