JS: Quote String
3 Ways to Quote a String
`GRAVE ACCENT quote`
→ best. 〔see Template String〕"QUOTATION MARK"
→ literal newline char not allowed.'APOSTROPHE quote'
→ basically same as QUOTATION MARK quote.
QUOTATION MARK Quote
Use QUOTATION MARK quote to enclose string.
"my cat"
Use \"
to embed a QUOTATION MARK in a QUOTATION MARK quoted string.
// deno-fmt-ignore console.log("my \"3\" cat" === `my "3" cat`);
APOSTROPHE Quote
APOSTROPHE Quote (U+27: APOSTROPHE) can also be used.
Use \'
for APOSTROPHE in a APOSTROPHE quoted string.
// APOSTROPHE 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 APOSTROPHE and QUOTATION MARK Quoted String
There's basically no difference, except that if you need to embed a APOSTROPHE or QUOTATION MARK , you need to escape it.
Literal Newline in QUOTATION MARK or APOSTROPHE Quoted String Not Allowed
Literal newline in QUOTATION MARK or APOSTROPHE 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 QUOTATION MARK or APOSTROPHE Quoted String Literal
The following are not allowed.
- 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)
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
Use \n
for newline.
console.log("cat\ndog"); /* prints cat dog */
〔see String Escape Sequence〕
JavaScript, String
- JS: String Overview
- JS: Quote String
- JS: Template String
- JS: String Escape Sequence
- JS: Unicode Escape Sequence
- JS: String Operations
- JS: Iterate String
- JS: String Code Unit
- JS: Count Chars in String 🚀
- JS: Tagged Template String
- JS: Regex Functions
- JS: Convert String, Number
- JS: String Object
- JS: String Constructor
- JS: String.prototype