1

I need to iterate over the characters of a string that may include emojis such as:

var s = "";

In javascript, each of these characters contain two code-points. The value of s.length will be 4. But I want to treat them as single characters. The new "for of" loop (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) does what I want.

var s = "";
alert( "length of " + s + " is " + s.length );
alert( s + " has " + numCharacters( s ) + " characters");

function numCharacters( s ) {
   var count = 0;
   for ( var ch of s ) {
        count++;
   }
   return count ;
}

However, it is not supported in IE. Any suggestions on how to iterate through the characters of a string that may contain emojis which would also work using Internet Explorer?

John Weidner
  • 2,055
  • 3
  • 18
  • 31

0 Answers0