Why quotes cause trouble
TypeScript lets you write a string literal with single quotes or double quotes. Both make the same kind of string. The quote character you open with is a signal: the string continues until the next quote of the same kind. Everything in between is text.
This causes trouble when the text itself contains a quote. Suppose you want to print My name's Andy and wrap it in single quotes:
console.log('My name's Andy');
The compiler reads the first ' as the start of a string and the apostrophe in name's as its end. The string is just 'My name'. The characters that follow, s Andy');, don't form valid code, so the compiler reports syntax errors. Your editor's colouring usually shows the problem too: the text after the apostrophe is coloured as code instead of as a string.
The fix: choose the other kind of quote
A string opened with " ends only at another ". A single quote inside it is an ordinary character:
console.log("My name's Andy");
My name's Andy
The same works the other way around. Text with double quotes inside goes in a single-quoted string:
console.log('Hey "bro"');
Hey "bro"
The broken version of that line, console.log("Hey "bro"");, fails for the same reason as the first example: the second " ends the string early and bro becomes stray code.
The rule is to wrap the text in whichever quote kind does not appear inside it.
When the text has both kinds
If the text contains both apostrophes and double quotes, neither choice works without help. The usual answer is a backslash escape, which is the topic of the next lesson in the chapter. Template literals, written with backticks, are another option, since they allow both kinds of quotes unescaped:
console.log(`It's "both" kinds of quotes`);
It's "both" kinds of quotes
For this lesson, switching the outer quotes is enough.
The exercise
The lesson asks for two lines of output, each solved with the quote-switching rule.
- Print
Let's meet at Wendy's place. The text has two apostrophes, which are single quotes, so wrap it in double quotes. - Print
I love the new "Avengers" movie. The text has double quotes, so wrap it in single quotes.
Each line is a console.log call, which prints its argument and then moves to a new line:
console.log("Let's meet at Wendy's place");
console.log('I love the new "Avengers" movie');
The output is:
Let's meet at Wendy's place
I love the new "Avengers" movie
Gotchas
- The quotes are not part of the output. They only mark where the string starts and ends.
console.log("Hi")printsHi. - Single and double quotes are equal.
'abc' === "abc"istrue. Many projects pick one style and use the other only when the text needs it, as the exercise does. - Don't mix the pair.
'text"is an error, because a string must close with the same kind of quote that opened it. - Typographic quotes are different characters. Curly quotes such as the ones a word processor inserts are not string delimiters in TypeScript. If you paste code and get an "invalid character" error, look for them.
- A string can't continue onto a new line. A regular quoted string that is missing its closing quote is an "unterminated string literal" error at the end of the line. A stray quote is therefore easy to spot, because the error appears right where the string was supposed to end.
Key takeaways
- A string ends at the next quote of the kind it started with.
- A quote of the other kind inside a string is plain text.
- Pick the outer quotes that the text doesn't contain.
- When the text contains both kinds, escape the quotes or use backticks.