Search code examples
javascriptunicodecharacter-encodingunicode-escapesunicode-literals

Cannot render js unicode character `\u1F310`


For example omega symbol rendered properly '\u03A9', but globe symbol '\u1F310' http://www.fileformat.info/info/unicode/char/1f310/index.htm - not. Was tried on console and node environments


Solution

  • '\u1F310'
    

    JavaScript \u escapes only take four digits, so this string literal is two code units, U+1F31 (Greek Small Letter With Dasia) and U+0030 (Digit Zero): ἱ0.

    JavaScript strings are made of 16-bit-wide code units, so the highest single code unit in JavaScript is \uFFFF and characters from U+10000 upwards, lying outside the Basic Multilingual Plane, cannot be referenced directly. To get them, you have to use a pair of UTF-16 surrogates. For U+1F310 that works out as '\uD83C\uDF10'.

    In ECMAScript 2015, there's a new string escaping form that takes the character number and works out the rest for you: '\u{1F310}'. This is available in recent versions of Node, but it will be a while before support is widespread enough in browsers.

    Printing the character to console is a separate challenge. As @MarcoS says you will need your terminal to be set to use a font that has a glyph for it. You will also need to be using a console that understands non-ASCII and in particular non-Basic-Multilingual-Plane characters. (If you're on the Windows command prompt, the only way to win that game is not to play.)