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 emojis after splitting (see example 1) is not an empty string but a zero-width joiner (ZWJ). The ZWJ acts as the glue between the single emojis, 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 (๐Ÿณ) + ZWJ + a rainbow (๐ŸŒˆ)!

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)

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *

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