JS: Unicode Escape Sequence

By Xah Lee. Date: . Last updated: .

Unicode Escape Sequence

Character in string can be represented by a escape sequence.

\u{hexadecimal}

represent a character by its Code Point in Hexadecimal

(new in ECMAScript 2015)

// represent any character by their Code Point in hexadecimal

console.log("\u{41}" === "A");

// can have leading 0, any number of
console.log("\u{00000041}" === "A");

// any unicode char works
console.log("\u{3b1}" === "α");

// the unicode can be more than 16 bits
console.log("\u{1F98B}" === "🦋");
// unicode escape can be used in identifiers

// deno-fmt-ignore
const \u{3b1} = 3;
console.log(α === 3);
// true
\u4_hexa_digits

Represent a character whose Code Point can be represented by 4 Hexadecimal digits or less.

This represents 2 bytes (16 bits, a code unit).

🛑 WARNING: 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:

// identifiers
// deno-fmt-ignore
const x\u{3b1} = 3;
console.log(xα === 3);
// true
// string literals
console.log("\u{3b1}" === "α");
// true
// string template literals
console.log(`\u{3b1}` === "α");
// true

🛑 WARNING: Unicode escape in regular expression literals

// unicode escape sequence in regular expression literals

console.log(/\u{3b1}/u.test("α"));
// true

// when using the form \u{...} , it must have the regex flag u
console.log(/\u{3b1}/.test("α"));
// false

// this form works everywhere
console.log(/\u03b1/.test("α"));
// true

Within a comment, Unicode escape sequence is ignored.

JavaScript. String

JavaScript Unicode topics