Walking a String by Code Point
In this lesson, you walk the team’s ticket titles one code point at a time in JavaScript, and turn each
code point into its number and back. In the last lesson, slice() cut the emoji in Zoë’s title in half,
because JavaScript strings count UTF-16 code units, and the emoji takes two of them: a surrogate pair.
The team lead’s Python walks the name with list() and a for loop. Python counts code points, so
list() gets five characters, and ord() gives each one its number:
name = "Zoë 😀"
print(list(name))
for char in name:
print(char, ord(char))
To cut a title safely, the JavaScript helper needs the same walk. Spread, from the last lesson, already
does what list() does: five code points, with the emoji whole.
The new part is the loop. Like spread, for...of asks the string for its values, and a string hands out
code points, not code units. So for char in name ports line for line, with const making a fresh char
each pass. Use of, not in: a loop written with in hands out positions instead.
Watch out: a code point isn’t always one character a person sees. An accent typed as its own code point, a flag, or a family emoji still comes apart into several, and a later lesson handles those.
There’s no standalone ord() in JavaScript. Instead, every string has a codePointAt() method, which
takes a position. Each char holds one code point, so position zero reads it:
const name = "Zoë 😀";
console.log([...name]);
for (const char of name) {
console.log(char, char.codePointAt(0));
}
The log matches Python’s line for line: Z is 90, the space is 32, and the emoji’s number, 128512, is
U+1F600 in hex, the emoji’s single code point, not its two code units:
[ 'Z', 'o', 'ë', ' ', '😀' ]
Z 90
o 111
ë 235
32
😀 128512
Here ord() stops matching. It takes a single character and refuses anything longer. codePointAt()
takes any string and a position, and that position counts code units, the same way slice()’s positions
do. In name, the emoji fills positions four and five:
const name = "Zoë 😀";
console.log(name.codePointAt(4));
console.log(name.codePointAt(5));
At position four, the start of the pair, codePointAt() combines both units and returns the emoji’s code
point, the same number as Python’s ord(name[4]). Python’s string ends there. Both say four only because
everything before the emoji is one unit; after an emoji, JavaScript’s positions run ahead of Python’s
indexes. Position five is the emoji’s second unit, and it logs 56832, not the emoji’s number:
128512
56832
That’s DE00, the low surrogate from the last lesson, on its own: a lone surrogate, not a character. Start
at a pair’s first unit and you get the whole emoji; land on its second and you get half. A walk with
for...of or spread never lands inside a pair.
Python’s chr() goes back the other way, from a number to text. JavaScript’s version is a static method
on String itself:
String.fromCodePoint(). Store the emoji’s number as point, and String.fromCodePoint() rebuilds the
emoji from it:
const name = "Zoë 😀";
const point = name.codePointAt(4);
const smile = String.fromCodePoint(point);
console.log(smile, [...smile]);
console.log(String.fromCodePoint(90, 111, 235));
Spread shows one code point, as list() does in Python. Here the analogy stops: chr() takes one number,
but String.fromCodePoint() takes as many as you pass, so 90, 111 and 235 join into Zoë:
😀 [ '😀' ]
Zoë
Python joins the characters itself, with "".join() over chr() of each number.
So in JavaScript, for...of and spread walk a string by code point, and an emoji’s two code units stay
together. codePointAt() reads the code point at a code unit position, like ord(); land inside a pair
and you get one half. And String.fromCodePoint() turns numbers back into text, like chr().
Next up is the exercise Truncate Ticket Titles Without Breaking Emoji. There, you’ll cut a ticket title to a limit counted in code points, not in the characters a person sees, without breaking an emoji.