Code units vs code points
isWellFormed (ES2024) returns false when a string contains a lone surrogate, half an emoji. The UTF-8 count is 1 + 4 + 1 bytes.
Every character has a Unicode number (a code point), but JavaScript stores strings as 16-bit pieces called UTF-16 code units, so length counts pieces, not characters: one emoji can have a length of 2, 4 or more. Use for...of, spread or Intl.Segmenter to work with real characters, and normalize() before comparing text.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write reverseText(s) that reverses a string without breaking emoji like 😀 (code points are enough here).
Write charsLeft(text, max) that returns how many visible characters are left, counting graphemes, so charsLeft("hi👍🏽", 10) returns 7.
Explain it without notes
Why is "😀".length equal to 2?
What is the difference between a code unit, a code point and a grapheme cluster?
How would you count the characters a user sees in a text box?
Why can two strings that look identical fail ===, and how do you fix it?
Preview
Console
Nothing logged yet.Expected console output
4
55357 56832
128512 1f600
true
4 3
false true
false b😀a
6 bytes in UTF-8