Loading video player…

Walking a String by Code Point

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:

Language: Python
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:

Language: JavaScript
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:

Language: Program Output
[ '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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
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ë:

Language: Program Output
😀 [ '😀' ]
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.

00:00 In this lesson, you’ll 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.

00:13 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, and Python counts code points.

00:27 So list() gets five characters, and ord() gives each one its number.

00:32 To cut a title safely, the JavaScript helper needs the same walk. So how does a JavaScript string hand out its code points?

00:41 Spread, from the last lesson, already does what list() does: it spreads the name into five code points.

00:49 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: for...of steps through the same five, with const making a fresh char each pass.

01:08 Use of, not in: a loop written with in hands out positions instead. The blank-looking fifth line is the space. Watch out: a code point isn’t always one character a person sees.

01:22 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.

01:33 Python’s loop uses ord() to print each character’s number. There’s no standalone ord() in JavaScript. Instead, every string has a codePointAt() method, which takes a position.

01:45 Each char holds one code point, so position zero reads it. The log matches Python’s line for line: Z is 90, and the space is 32. The emoji’s number is the big one at the end, U+1F600 in hex.

02:02 That’s one code point, not two code units.

02:07 Here the ord() analogy stops. ord() 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.

02:22 The emoji fills positions four and five. At position four, the pair’s start, codePointAt() combines both units into the emoji’s code point. Python’s ord(name[4]) gives the same number, but only because everything before the emoji is one unit.

02:40 Position five is the emoji’s second unit. The emoji’s number again, nothing, or something else? Your turn: pause the video and predict.

02:55 Not the emoji, and not nothing: it logs 56832.

03:03 That’s DE00, last lesson’s low surrogate, alone: a lone surrogate. Land inside a pair and you get half an emoji; a walk with for...of or spread never lands there.

03:18 Python’s chr() goes back the other way, from a number to text. JavaScript’s version is String.fromCodePoint(), a static method on String itself.

03:29 Store the emoji’s number as point, and String.fromCodePoint() rebuilds the emoji from it. Spread shows one code point, as list() does for the Python string.

03:40 Here the analogy stops: chr() takes one number, but String.fromCodePoint() takes as many as you pass. Give it 90, 111 and 235, and it joins them into Zoë.

03:53 In Python, you join the characters yourself, with a generator over the three numbers.

04:01 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, as ord() does; land inside a pair and you get one half.

04:17 And String.fromCodePoint() turns numbers back into text, the way chr() does. Next up is the exercise Truncate Ticket Titles Without Breaking Emoji.

04:29 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.

Become a Member to join the conversation.