The payments team’s branch-name helper worked fine on English ticket titles. Then the Lisbon office filed “Zoë’s 😀 refund fix”, and the branch came out with a broken symbol where the emoji should be. The team lead’s Python version never did that. It counted characters with len() and cut with slicing, and the JavaScript port does the same with .length and .slice().
So why does one emoji survive in Python and break in JavaScript? This lesson shows what a JavaScript string counts and how to pick the right measure before you cut. The team lead reduced the failing title to a test string holding only its trouble spots, an accented letter and an emoji: Zoë 😀.
Port the Character Count From Python
Here’s the team lead’s Python check. Keeping five characters stands in for the helper’s length limit, and the cutoff lands right after the emoji:
name = "Zoë 😀"
print(len(name))
print(name[:5])
Python counts three letters, a space, and the emoji, so it prints 5, and the slice keeps Zoë 😀 intact.
As a Pythonista, you port the code line for line. A variable declared with const can’t be reassigned, and the port uses .length where Python uses len() and .slice(0, 5) where it uses name[:5]:
const name = "Zoë 😀";
console.log(name.length);
console.log(name.slice(0, 5));
Before you run it, predict how long the name is. Here’s what JavaScript prints:
6
Zoë �
JavaScript counts six of something, and the emoji is gone. In its place is a question mark in a diamond, the symbol that broke the branch name.
See the Surrogate Pair Inside the Emoji
The six comes from what a JavaScript string is made of. A Python str is a sequence of code points, the numbers Unicode gives each character, so len() counts code points. A JavaScript string is a sequence of UTF-16 code units, 16-bit numbers, and .length counts those. Square-bracket indexing and the positions you give .slice() count code units too.
The 16 bits date from 1995, when JavaScript appeared and every Unicode character fit in one 16-bit unit. Unicode later outgrew that, and JavaScript strings kept their units so existing code wouldn’t break.
One unit holds values up to U+FFFF. Z, o, this ë (typed as a single code point), the space, and each character of the 東京 in the Tokyo office’s titles fit in one unit. The emoji 😀 is code point U+1F600, which is too large. UTF-16 stores it as a surrogate pair, two code units from a range Unicode reserves for this job. The high surrogate, D83D, comes first, followed by the low surrogate, DE00.
So the six is four units for Zoë plus two for the emoji. When .slice(0, 5) stops after the fifth unit, it keeps the high surrogate and drops the low one. Spreading a string into an array splits it into code points, like list(name) in Python, so it shows what the slice kept:
const name = "Zoë 😀";
console.log([...name.slice(0, 5)]);
The last element is the high surrogate on its own. Its partner was cut off:
[ 'Z', 'o', 'ë', ' ', '\ud83d' ]
A lone surrogate like this isn’t a character. It can’t be written out as UTF-8, so console.log(name.slice(0, 5)) prints the replacement character � instead.
Python’s str can hold a lone surrogate too, but printing one to a UTF-8 stream raises UnicodeEncodeError unless the stream is set to escape or replace it. JavaScript’s .slice() and console.log() don’t throw an exception. The damaged string travels on into the branch name until a stricter step, such as URL encoding, throws on it.
Pick the Ruler for the Limit
The fix starts with measuring. Spreading the string into an array lets you count code points, as Python’s len() does:
const name = "Zoë 😀";
console.log(name.length);
console.log([...name].length);
The rulers differ by the emoji’s extra unit:
6
5
As a JavaScript developer, you pick the ruler based on the requirement. .length counts code units, the measure for .slice() positions and UTF-16 limits, but a cut must never split a pair. The length of the spread array counts code points, matching Python’s len() and a limit like “at most five code points”.
Two more rulers exist, and this course gets to both. You see graphemes. A family emoji such as 👨👩👧 is one symbol on screen, yet Python’s len() gives 5, for three people joined by two invisible joiner code points. Display limits mean graphemes. Storage limits, like the git host’s, often mean UTF-8 bytes, a fourth measure.
So when a helper says “characters”, ask which ruler it means. For this lesson’s five-code-point limit, check with [...name].length, not .length. The host’s byte budget needs the UTF-8 ruler, which a later lesson measures.
In the next lesson, Walking a String by Code Point, you’ll walk the name one code point at a time, inspect its emoji’s boundary, and rebuild a code point from its number with JavaScript’s counterparts of Python’s ord() and chr().