JS: Unicode Escape Sequence
Unicode Escape Sequence
Character in string can be represented by a escape sequence.
\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
\u4_hexa_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" === "Ξ±");
Where Can Unicode Escape Sequence be Used
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.
JavaScript, String
- JS: String Overview
- JS: Quote String
- JS: Template String
- JS: String Escape Sequence
- JS: Unicode Escape Sequence
- JS: String Operations
- JS: Iterate String
- JS: String Code Unit
- JS: Count Chars in String π
- JS: Tagged Template String
- JS: Regex Functions
- JS: Convert String, Number
- JS: String Object
- JS: String Constructor
- JS: String.prototype