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

\"

U+22: QUOTATION MARK

(the 22 is Code Point in hexadecimal, followed by the character name.)

\'

U+27: APOSTROPHE

\\

U+5C: REVERSE SOLIDUS

\b

U+08: BACKSPACE

\f

U+0C: FORM FEED

\n

U+0A: LINE FEED

\r

U+0D: CARRIAGE RETURN

\t

U+09: CHARACTER TABULATION

\v

U+0B: LINE TABULATION

Unicode escape sequence

\u…

the escape characters are mostly standard control characters. See ASCII Characters

Backslash other

Backslash in front of other character quote the char literally:

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

Backslash at end of line

Backslash in front of literal newline means continue the line:

console.assert( "a\
b\
c" === "abc" )

Literal tab and other

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

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