JS: String Escape Sequence

By Xah Lee. Date: . Last updated: .

What is Escape Sequence

Escape sequence are sequence of characters starting with backslash, inside a string, to represent certain unprintable characters such as \n for Line Feed to represent newline, or to represent Unicode characters.

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

\"
QUOTATION MARK
\'
APOSTROPHE
\\
REVERSE SOLIDUS
\b
BACKSPACE γ€”see ASCII Characters〕
\f
FORM FEED
\n
LINE FEED
\r
CARRIAGE RETURN
\t
CHARACTER TABULATION
\u…
Unicode Escape Sequence
\v
LINE TABULATION

Backslash

Backslash in front of other character quote the char literally:

"\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" === "	");

JavaScript, String