Use double straight quote (U+22: QUOTATION MARK) to enclose string.
\" for double quote in a double quoted string.
// deno-fmt-ignore console.log("my \"3\" cat" === `my "3" cat`);
Single Quote (U+27: APOSTROPHE) can also be used.
\' for single quote in a single quoted string.
// single quoted string // deno-fmt-ignore console.log('my cat'); // my cat // deno-fmt-ignore // with escape console.log('john\'s cat'); // john's cat
Difference Between Double and Single Quoted String
There's basically no difference, except that if you need to include a double/single quote, you need to escape it if it's the same delimiter.
Literal newline in double/single quoted string not allowed
Literal newline in double/single quoted string is not allowed. Use backslash to continue a line.
illegal syntax. Literal newline is not allowed let xx = "a b"; error: The module's source code could not be parsed
Characters Not Allowed in Double Quoted or Single Quoted String Literal
The following are not allowed in double quoted or single quoted string literal.
- U+005C REVERSE SOLIDUS (aka backslash)
- U+000D CARRIAGE RETURN (aka line return)
- U+2028 LINE SEPARATOR
- U+2029 PARAGRAPH SEPARATOR
- U+000A LINE FEED. (aka newline, line return)
These are unprintable ASCII Characters.
If you include them without escape, you get syntax error.
"a b" SyntaxError: Invalid or unexpected token
to include these characters, use String Escape Sequence, or Template String .
Use Backslash to Continue a Line
let xx = "c\ d"; console.log(xx === "cd");
String Escape Sequence
\n for newline.
let ss = "cat\ndog"; console.log( ss === `cat dog`, );
[see String Escape Sequence]
template string is a new in 2015. It lets you have literal newline in string, and variable and expressions in string.
- String Overview
- Quote String
- Template String
- String Escape Sequence
- Unicode Escape Sequence
- String Operators/Function/Methods
- Iterate Chars in String
- String Code Unit
- Count Chars in String 🚀
- Tagged Template String
- Regex Functions
- Convert String/Number