Search code examples
javascriptunicode

How to split Unicode string to characters in JavaScript


For long time we used naive approach to split strings in JS:

someString.split('');

But popularity of emoji forced us to change this approach - emoji characters (and other non-BMP characters) like πŸ˜‚ are made of two "characters'.

String.fromCodePoint(128514).split(''); // array of 2 characters; can't embed due to StackOverflow limitations

So what is modern, correct and performant approach to this task?


Solution

  • Using spread in array literal :

    const str = "πŸŒπŸ€–πŸ˜ΈπŸŽ‰";
    console.log([...str]);

    Using for...of :

    function split(str){
      const arr = [];
      for(const char of str)
        arr.push(char)
       
      return arr;
    }
    
    const str = "πŸŒπŸ€–πŸ˜ΈπŸŽ‰";
    console.log(split(str));