Code
Description
Example
\uXXXX
Arbitrary Unicode code point (where +XXXX+ is a
hexadecimal code point)
"De Morgan’s law: \u2310(P \u22c0
Q) \u21D4 (\u2310P) \u22c1
(\u2310Q)"
\xXX
Latin-1 character (where +XX+ is a hexadecimal Latin-1
code point)
"\xc9p\xe9e is fun, but foil is
more fun."
Note that the Latin-1 character set is a subset of Unicode, and any Latin-1 character
\xXX
can be represented by the equivalent Unicode code point
\u00XX
. For hexadeci‐
mal numbers, you may use lowercase or uppercase letters as you please; I personally
favor lowercase, as I find them easier to read.
You don’t need to use escape codes for Unicode characters; you can also enter them
directly into your editor. The way to access Unicode characters varies among editors
and operating systems (and there is usually more than one way); please consult your
editor or operating system documentation if you wish to enter Unicode characters
directly.
Additionally, there are some rarely used special characters, shown in
. To my
recollection, I have never used any of these in a JavaScript program, but I include
them here for the sake of completeness.
Table 3-2. Rarely used special characters
Code Description
Example
\0
The NUL character (ASCII/Unicode 0)
"ASCII NUL: \0"
\v
Vertical tab (ASCII/Unicode 11)
"Vertical tab: \v"
\b
Backspace (ASCII/Unicode 8)
"Backspace: \b"
\f
Form feed (ASCII/Unicode 12)
"Form feed: \f"
Template Strings
A very common need is to express values in a string. This can be accomplished
through a mechanism called string concatenation:
let
currentTemp
=
19.5
;
// 00b0 is the Unicode code point for the "degree" symbol
const
message
=
"The current temperature is "
+
currentTemp
+
"\u00b0C"
;
Up until ES6, string concatenation was the only way to accomplish this (short of
using a third-party library). ES6 introduces string templates (also known as string
42 | Chapter 3: Literals, Variables, Constants, and Data Types