JavaScript: String Escape Sequence

By Xah Lee. Date: . Last updated: .

Backslash sequence such as \n is called escape sequence.

console.log("a\nb");
// prints a and b each on its own line
// the \n is a escape sequence. It means newline

List of Escape Sequences

\b
BACKSPACE
\t
CHARACTER TABULATION
\n
LINE FEED
\v
LINE TABULATION
\f
FORM FEED
\r
CARRIAGE RETURN
\"
QUOTATION MARK
\'
APOSTROPHE
\\
REVERSE SOLIDUS

Backslash in front of other character quote the char literally:

console.log("\l\ov\e" === "love");

Backslash in front of literal newline means continue the line:

console.log(
"a\
b\
c" === "abc",
);

Some of the escape sequence character can be in string literal directly without escaping.

// \t (tab character) can be in string literally
console.log("\t" === "	");

Characters Not Allowed in String Literal

The following are not allowed in string literal. They must be escaped.

if you include them without escape, you get syntax error.

console.log(
"a
b"
);
// SyntaxError: Invalid or unexpected token

Unicode Character Escape Sequence

Unicode Escape Sequence

JavaScript String

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc