Start learning free

LESSON 2 OF 348 · WELCOME TO TYPESCRIPT

Introduction to Strings in TypeScript: Quotes and Text

Lesson Read the full explanation

TL ; DR:

  • To create a string in TypeScript, we put some text inside single quotes
    ('Hello world') or double quotes ("Hello world").


  • Be careful not to forget any quote! This code missing a " at the end:

    console.log("Welcome!);

    would throw a SyntaxError



Full lesson:

How boring would our life as humans be if we communicated only using numbers? Luckily we have letters, words and languages to express what we think!

We can also use letters and words in TypeScript to express more meaningful messages like we do in real life.


Strings:

In programming, a string is any block of text e.g. any sequence of characters from your keyboard (letters, numbers, spaces, symbols, etc.).

To create a string in TypeScript, we put some text inside single quotes ('Hello world') or double quotes ("Hello world").

For example, we can use strings inside the console.log() function for printing messages to the console:

console.log('My name is Andy');
console.log("Welcome to AlgoCademy!");

The output of this code is:

My name is Andy
Welcome to AlgoCademy!


As you can see, the quotes are not printed. That's because quotes are not part of the string. Their job is solely to let TypeScript know when a string declaration starts and when it ends.


Assignment
Follow the Coding Tutorial and let's work with strings!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · WELCOME TO TYPESCRIPT

    Introduction To Strings in TypeScript, explained

    What a string is

    A string is a piece of text: any sequence of characters you can type, including letters, digits, spaces and symbols. Programs use strings for names, messages and anything else made of text. In TypeScript the type of a string is string.

    You create a string by putting text inside quotes. Single quotes and double quotes work the same way:

    console.log('My name is Andy');
    console.log("Welcome!");
    
    My name is Andy
    Welcome!
    

    The quotes are not part of the string. Their job is to tell the compiler where the text starts and where it ends, so they are not printed.

    Single or double quotes

    Pick one style and use it consistently. The choice matters only when the text itself contains a quote. If the text has an apostrophe, wrap it in double quotes, and if it has double quotes, wrap it in single quotes:

    console.log("It's fine");
    console.log('She said "hi"');
    
    It's fine
    She said "hi"
    

    You can also keep your usual quotes and put a backslash before the quote inside the text, which tells TypeScript the quote belongs to the string. 'It\'s fine' prints the same as the first line above. The backslash is an escape character: \n starts a new line, and \\ prints one backslash.

    console.log("line1\nline2");
    
    line1
    line2
    

    Strings versus numbers

    Quotes change what a value is. 1998 is a number and "1998" is text that happens to contain digits. They print identically, but they behave differently:

    console.log(1998 + 2);
    console.log("1998" + 2);
    console.log(typeof "5", typeof 5);
    
    2000
    19982
    string number
    

    With a string on either side, + joins text instead of adding. Joining is called concatenation. For example, "Hello" + " " + "world" gives Hello world.

    A few things strings can do

    Every string knows its own length and can be asked for single characters or for changed copies of itself:

    console.log("Hello".length, "".length, "a b".length);
    console.log("abc"[0], "abc".toUpperCase());
    
    5 0 3
    a ABC
    

    A space counts as a character, and an empty string "" is a valid string with length 0. Strings cannot be changed in place. "abc".toUpperCase() returns a new string, and writing something like s[0] = "b" on a string variable is rejected by the compiler with an error saying the index signature only permits reading.

    Backticks

    TypeScript has a third kind of quote, the backtick. A string in backticks is a template literal, and it can contain expressions inside ${}, which are replaced by their values:

    console.log(`2 + 3 = ${2 + 3}`);
    
    2 + 3 = 5
    

    For plain text, single or double quotes are enough. Backticks are worth reaching for when a message mixes text and values.

    Common mistakes

    • A missing closing quote. console.log("Welcome!); never ends its string, so the compiler reports an unterminated string literal. Every opening quote needs a matching closing one of the same kind.
    • Mixing quote kinds. 'Hey there!" starts with a single quote and ends with a double quote, so the string never closes.
    • Forgetting the quotes. console.log(Hello) looks for a variable named Hello. Without quotes, text is treated as a name.
    • Quoting a number by accident. "31" prints like 31 but cannot be used like a number without being converted.

    The exercise

    The exercise asks you to print the text I love coding!:

    1. Write console.log( to start the call.
    2. Type the text inside a pair of double quotes, exactly as given, including the capital letter, the space between words and the exclamation mark.
    3. Close the parenthesis and add a semicolon.

    The finished line is console.log("I love coding!");. The lesson's tests compare the output with the expected text, so a different capital letter or a missing space will fail.

    Key takeaways

    • A string is text, written between single or double quotes.
    • The quotes are not part of the string and are not printed.
    • A quote kind inside the text needs the other kind around it, or a backslash in front of it.
    • 1998 is a number, "1998" is a string, and + treats them differently.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free