JavaScript: Unicode Escape Sequence
Character in string can be
represented by a
escape sequence.
For example,
"\u0041"
is the same as "A"
.
There are 2 syntax:
\u4_hexd_digits
-
represent a character whose Codepoint
can be represented by 4
Hexadecimal
digits or less.
Must pad 0 in front if not 4 digits
// represent a char by its codepoint in hexadecimal // must pad 0 in front if not 4 digits console.log("\u0041" === "A"); console.log("\u03b1" === "Ξ±");
\u{hexadecimal}
-
represent any character whose Codepoint is the Hexadecimal
(new in ES2015)
// represent any character by their Codepoint console.log("\u{41}" === "A"); // if 0 padded, still works console.log("\u{000000000041}" === "A"); // any unicode char works console.log("\u{3b1}" === "Ξ±"); console.log("\u{1f602}" === "π");
// unicode escape can be used in identifiers // deno-fmt-ignore const \u{3b1} = 123; console.log(Ξ± === 123);
Unicode escape sequence can be used in:
- string literals
- regular expression literals
- string template literals
- identifiers
- any function that takes argument of Unicode codepoint can also be replaced by Unicode escape sequence.
Within a comment, Unicode escape sequence is ignored.