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! πŸ€“

Elsewhere ,

2 Responses to Fun with JavaScript and Emoji

  1. Pingback: Fun with JavaScript and emoji - murze.be

  2. Pingback: runes – Unicode-aware JS string splitting with full Emoji support | Bram.us

Leave a Reply

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