Unicode Characters & Escape Sequences
An escape sequence is a backslash followed by one or more characters, which together stand for a single character in a string. Escapes let a string contain characters that would otherwise end it or be hard to type, such as a quote mark, a line break or a letter from another alphabet.
JavaScript strings hold Unicode text, so any character in any language can appear in one, either typed directly or written as an escape with its Unicode number. The Unicode code charts list every character with its number.
Common escape sequences
Section titled “Common escape sequences”| Escape | Character |
|---|---|
\n |
line break, or new line |
\t |
tab |
\' |
single quote |
\" |
double quote |
\` |
backtick |
\\ |
backslash |
\r |
carriage return, which Windows puts before \n at the end of a line |
\0 |
the null character, number 0 |
A quote needs escaping only inside a string that uses the same quote mark. A backslash before a character with no special meaning is simply dropped, so '\q' is just 'q'.
console.log('Name:\tAsha\nCity:\tLeeds');console.log('It\'s raining');console.log("She said \"hello\"");console.log('A backslash: \\');console.log(`Backticks need escaping here: \`, but ' and " do not`);This prints:
Name: AshaCity: LeedsIt's rainingShe said "hello"A backslash: \Backticks need escaping here: `, but ' and " do notCharacters by number
Section titled “Characters by number”Unicode numbers are usually written in hexadecimal, such as U+00E9 for é. JavaScript has three escapes that take such a number:
\xHHtakes exactly two hex digits, so it reaches characters up to U+00FF.\uHHHHtakes exactly four hex digits, which covers most of the world’s alphabets.\u{H…}takes from one to six hex digits in braces, which reaches every Unicode character, including emoji.
console.log('\xA3' + '5');console.log('café');console.log('Ω = ohm');console.log('\u{1F34E} apple');console.log('\u{1F34E}' === '🍎');This prints:
£5caféΩ = ohm🍎 appletrueThe last line shows that a character above U+FFFF, such as the apple emoji, is stored as two 16-bit units called a surrogate pair. Before \u{…} was added in ES2015, the only way to write such a character was as its two halves.
Length and code points
Section titled “Length and code points”A string’s length counts those 16-bit units, not the characters a reader sees, so an emoji adds 2 to it. Spreading a string into an array, or looping over it with for...of, splits it by whole characters instead.
const label = 'Tea \u{1F375}';
console.log(label, label.length, [...label].length);console.log(label.charCodeAt(4).toString(16), label.codePointAt(4).toString(16));console.log(String.fromCharCode(0x00e9), String.fromCodePoint(0x1f375));console.log('é'.codePointAt(0), '£'.codePointAt(0).toString(16));This prints:
Tea 🍵 6 5d83c 1f375é 🍵233 a3charCodeAt() and String.fromCharCode() work in 16-bit units, so at index 4 charCodeAt() returns only the first half of the emoji. codePointAt() and String.fromCodePoint() work with whole characters and are the ones to use. charCodeAt vs codePointAt compares them in detail.
Some characters that look like one are built from several code points, such as a flag or an emoji with a skin tone, so even [...label].length can overcount them. Intl.Segmenter splits text the way a reader would, where that matters.
The same text, two ways
Section titled “The same text, two ways”Some accented letters can be stored either as one code point or as a plain letter followed by a combining accent. Both look identical, but they are different strings.
const typed = 'café';const pasted = 'café';
console.log(typed, pasted, typed === pasted);console.log(typed.length, pasted.length);console.log(typed.normalize() === pasted.normalize());This prints:
café café false4 5truenormalize() converts a string to one standard form, by default the composed one, so call it on text from different sources before comparing or searching it.
Steve Griffith covers this topic in his video JavaScript Unicode Characters and Escape Sequences.