Skip to content Skip to sidebar Skip to footer

Javascript Literals For Characters Higher Than U+ffff

My javsacript source code is strictly ascii and I want to represent the anger symbol in a string literal. Is that possible in javascript?

Solution 1:

JavaScript strings are effectively UTF-16, so you can write the surrogate pair using Unicode escapes: "\uD83D\uDCA2" (this is what's shown on that page for the Java source code, which also works in JavaScript).

As of ES2015 (ES6), you can also write it as \u{1F4A2} rather than working out the surrogate pairs (spec).

Example:

Using \uD83D\uDCA2:

document.body.innerHTML =
  "(Of course, this only works if the font has that character.)" +
  "<br>As \\uD83D\\uDCA2: \uD83D\uDCA2";

Using \u{1F4A2}(if your browser supports the new ES2015 feature):

document.body.innerHTML =
  "(Of course, this only works if the font has that character.)" +
  "<br>As \\u{1F4A2}: \u{1F4A2}";

Here's an example using U+1D44E (\uD835\uDC4E, \u{1D44E}), the stylized a used in mathematics, which shows here on SO for me on Linux (whereas your :anger: emoji doesn't, but does if I use Windows 8.1):

Using \uD835\uDC4E:

document.body.innerHTML =
  "(Of course, this only works if your browser has that symbol.)" +
  "<br>As \\uD835\\uDC4E: \uD835\uDC4E";

Using \u{1D44E}(if your browser supports the new ES2015 feature):

document.body.innerHTML =
  "(Of course, this only works if your browser has that symbol.)" +
  "<br>As \\u{1D44E}: \u{1D44E}";

Post a Comment for "Javascript Literals For Characters Higher Than U+ffff"