...

/

Discussion: How Long Is a Pirate Flag?

Discussion: How Long Is a Pirate Flag?

Execute the code to understand the output and gain insights into how emojis are displayed as connected sequences and how their length is calculated in JavaScript.

Verifying the output

Now, it’s time to execute the code and observe the output.

Press + to interact
// Happy Flag Day!
const flag = "🏴‍☠";
console.log(flag.length);

Understanding the output

You might have anticipated that the output would be 1, but this code will actually output:

4

Emoji zero-width joiner (ZWJ)

The length of emojis may not be what you expect. In JavaScript, some characters and emojis are formed by multiple code units, which can lead to unexpected results when using the length property. The pirate flag emoji is called an emoji ZWJ sequence, and it’s made up of two separate emojis:

🏴‍☠ = 🏴 + ☠ ...