Skip to content
Justice Tech School

Unicode, Emoji and localeCompare

Strings are UTF-16, so some characters use two units. Use the spread operator or Array.from to count characters, normalize for accents and localeCompare to sort text correctly.

Example
const s = "Hi 😀";
console.log(s.length, [...s].length);
console.log(["é", "e", "z"].sort(), ["é", "e", "z"].sort((a, b) => a.localeCompare(b)));

Exercise

Compare for sorting text.

a.(b)

Quiz Test

How can you count visible characters?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.