javascriptstringindexofcjkcharat

Javascript indexOf/charAt not working for Japanese half-width Katakana


In my codebase I have this code, surprisingly it returns 1:

'トゲ'.indexOf('ケ') // Returns 1

The character doesn't seem to appear in string トゲ.

I also tried to run this code:

'トゲ'.charAt(1)  // Returns `ケ`

And to my surprise it returns .

Can you explain why the codes above return 1 and ? Thank you!


Solution

  • Your string consists of three characters: ト, ケ and ゙. There is no dedicated halfwidth-ge character, you'll always use a regular halfwidth character and add the halfwidth voiced sound mark to it, which will be rendered as the combined glyph ゲ.

    Note that there is a dedicated fullwidth ゲ KATAKANA LETTER GE U+30B2.