Aside from letters, numbers, and symbols, there is another class of characters
that we will occasionally use in strings, known as special characters.
These characters consist of special character combinations that all begin with
\ (backslash). They allow us to include characters in strings that would
be difficult or impossible to include otherwise, such as Unicode characters
that are not on our keyboards, control characters, and whitespace characters.
The most commonly-used special characters are
\t, which are the
newline and tab characters, respectively. They work as you would expect.
console.log("A message\nbroken across lines,\n\tand indented");
A message broken across lines, and indented
We can also represent Unicode characters (most of which aren’t on a normal keyboard) using special character combinations of the form
\uXXXX, where the Xs are combinations referenced by the
. This allows us to use character sets other than the basic Latin characters that English is based on, such as Greek, Cyrillic, and Arabic, as well as a wider array of symbols.
console.log("The interrobang character, \u203d, combines ? and !");
The interrobang character, ‽, combines ? and !
We can also use the backslash,
\, to include quotes within a string. This
is known as escaping a character.
console.log("\"The dog's favorite toy is a stuffed hedgehog,\" said Chris");
"The dog's favorite toy is a stuffed hedgehog," said Chris
Check Your Understanding
Which of the options below prints
'Code' on separate