Use double straight quote " (U+22: QUOTATION MARK) to enclose string.
console.log( "Mary's cat" ); // Mary's cat
\" for double quote in a double quoted string.
Single Quote ' (Use double straight quote ) can also be used.
\' for single quote in a single quoted string.
// single quote, with escape console.log( 'Mary\'s cat' ); // Mary's cat
What's the 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 s = "a b"; // error: The module's source code could not be parsed: Expected ';', '}' or <eof>
use backslash to continue a line
let s = "c\ d"; console.log(s); // prints cd
String Escape Sequence
\n for newline.
let ss = "cat\ndog"; console.log(ss); // prints 2 lines: // 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
- Tagged Template String
- Regex Functions
- Convert String/Number