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
- CHARACTER TABULATION
- LINE FEED
- LINE TABULATION
- FORM FEED
- CARRIAGE RETURN
- QUOTATION MARK
- 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.
- U+005C (REVERSE SOLIDUS)
- U+000D (CARRIAGE RETURN)
- U+2028 (LINE SEPARATOR)
- U+2029 (PARAGRAPH SEPARATOR)
- U+000A (LINE FEED).
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
- String Overview
- Quote String
- Template String
- String Escape Sequence
- Unicode Escape Sequence
- String Operators/Function/Methods
- Iterate Chars in String
- String Code Unit
- Tagged Template String
- Regex Functions
- Convert String/Number