Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use five digit long Unicode characters in JavaScript

In JavaScript I can do this:

foo = "\u2669" // 1/4 note

But I can't do this

foo = "\u1D15D" // full note  -five hex digits

It will be interpreted as "\u1D15" followed by "D"

Are there any workarounds for this?

UPDATE 2012-07-09: The proposal for ECMAScript Harmony now includes support for all Unicode characters.

like image 826
itpastorn Avatar asked May 16 '12 09:05

itpastorn


People also ask

Can I use Unicode in JavaScript?

Unicode in Javascript source codeIn Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.

How do I use Unicode character codes?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

Does JavaScript use UTF-8?

Most developers just save their JavaScript as UTF-8 unknowingly in almost all cases, which works fine. But JavaScript has to "decode" those from UTF-8 to UTF-16 for its own internal use, especially if your scripts have upper plane Unicode characters inside it.

What is the tallest Unicode character?

The tallest Unicode character in the current standard (Unicode 6.1) is 🗻 , U+1F5FB MOUNT FUJI , which is 3776 meters tall.


2 Answers

Try putting the unicode between curly braces: '\u{1D15D}'.

like image 83
Manuel Dipre Avatar answered Oct 23 '22 09:10

Manuel Dipre


In the MDN documentation for fromCharCode, they note that javascript will only naturally handle characters up to 0xFFFF. However, they also have an implementation of a fixed method for fromCharCode that may do what you want (reproduced below):

function fixedFromCharCode (codePt) {
    if (codePt > 0xFFFF) {
        codePt -= 0x10000;
        return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF));
    }
    else {
        return String.fromCharCode(codePt);
    }
}

foo = fixedFromCharCode(0x1D15D);
like image 22
mpdaugherty Avatar answered Oct 23 '22 08:10

mpdaugherty