Fun with JavaScript and Emoji

emoji-spread

Today Wes Bos tweeted a few fun things one can do with JavaScript and Emoji:

  1. It’s possible to spread emoji sequences into their single parts:

    [...'๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ']
    // ["๐Ÿ‘จ", "โ€", "๐Ÿ‘ฉ", "โ€", "๐Ÿ‘ง", "โ€", "๐Ÿ‘ฆ"]
  2. Combining Emoji is also possible:

    ["๐Ÿ‘จ", "โ€", "๐Ÿ‘ฉ", "โ€", "๐Ÿ‘ง"].reduce((prev, curr) => prev + curr)
    // "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง"
  3. And oh, you can even replace single emoji within emoji units, yielding a new emoji unit:

    '๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆ'.replace(/๐Ÿ‘ฉ/g, '๐Ÿ‘จ')
    // "๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆ"
  4. This kind of sorcery also works with skin tone modifiers by the way:

    [...'๐Ÿ›€๐Ÿพ']
    // ["๐Ÿ›€", "๐Ÿพ"]
    
    ["๐Ÿ›€", "๐Ÿพ"].reduce((prev, curr) => prev + curr)
    // "๐Ÿ›€๐Ÿพ"
    
    '๐Ÿ›€๐Ÿพ'.replace("๐Ÿพ", "")
    // "๐Ÿ›€"

Digging deeper into this it came to my attention that the extra character in between the single emoji after splitting (see example 1) is not an empty string but a zero-width joiner (ZWJ). The ZWJ ("\u200d") acts as the glue between the single emoji, yielding the correct combined emoji sequence.

The full list of Emoji ZWJ Sequences contains all possible sequences, and holds some (toe me) nice surprises. Take the rainbow flag emoji (๐Ÿณ๏ธโ€๐ŸŒˆ) for example: that actually is a combination of a white flag emoji and a rainbow emoji!

'๐Ÿณ' + '\u200d' + '๐ŸŒˆ'
// "๐Ÿณ๏ธโ€๐ŸŒˆ"

Geekfun! ๐Ÿค“

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

โ˜•๏ธ Buy me a Coffee ($3)

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more โ€ฆ)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Join the Conversation

3 Comments

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.