Start learning free

LESSON 58 OF 348 · STRINGS I

Quotes Inside Quotes in TypeScript

Lesson Read the full explanation

In our introduction to strings, we learned that we can use either single quotes or double quotes to create / declare a string.

Well, sometimes you'll have to choose between these two to not run into issues. Those situations are when our text contains quotation marks.

For example, suppose we want to print the string My name's Andy. If we surrond the text by single quotes:

console.log('My name's Andy');

This code would produce a SyntaxError

You can also see by the text color that we have a problem. TypeScript reads the first apostrophe and interprets that a string declaration starts there. Then it starts parsing through characters until it finds the second apostrophe and interprets that the string declaration ends there.

So it concludes that the string is 'My name' but then it cannot understand the code coming after that: s Andy');.

We can solve this by using double quotes to wrap up the text:

console.log("My name's Andy");

The output of this code is:

My name's Andy

A string declaration starting with a " can be ended only by another ". And so the interpreter will not stop when reaching the ' and will successfully read the whole string.

The same thing happens if we want to have double quotes as part of the string's content:

Surrounding by double quotes doesn't work:

console.log("Hey "bro""); // SyntaxError

Surrounding by single quotes works:

console.log('Hey "bro"'); // Output: Hey "bro"

Assignment
Follow the Coding Tutorial and let's practice with quotes!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS I

    Quotes Inside Quotes in TypeScript, explained

    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.

    1. Print Let's meet at Wendy's place. The text has two apostrophes, which are single quotes, so wrap it in double quotes.
    2. 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") prints Hi.
    • Single and double quotes are equal. 'abc' === "abc" is true. 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.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free