Start learning free

LESSON 8 OF 348 · WELCOME TO TYPESCRIPT

String Concatenation in TypeScript: Joining Text with +

Lesson Read the full explanation

We can concatenate strings with the plus operator (+).

Here is a program that concatenates the strings "I love coding. " and "It is fun!" and assigns the resulted string to a variable message:

let message = "I love coding. " + "It is fun!";
console.log(message);

Output:

I love coding. It is fun!

Concatenation with variables:

String concatenation is redundat when used with string literals because we already know what the resulted string would be.

For example, in our program, we could have initialized message directly with the bigger string instead of doing concatenation:

let message = "I love coding. It is fun!";
console.log(message);

String concatenation is used to dynamically generate messages that can be different depending on context.

For example, I might want to print a specific message about my favorite animal, whatever that is:

let favoriteAnimal = "parrot";

let message = "My favorite animal is the " + favoriteAnimal;

console.log(message);

Output:

My favorite animal is the parrot

The value of message will differ if my favorite animal was "dog" or "cat", but it would still make sense.


Concatenating multiple strings:

We can concatenate as many strings as we want in the same line of code by adding a + before each string:

let animal = "parrot";
let name = "Andy";

console.log(name + ", that's a nice " + animal + " you got there!");

Output:

Andy, that's a nice parrot you got there!

Be careful to add whitespaces and other characters you want in your message


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


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · WELCOME TO TYPESCRIPT

    String Concatenation in TypeScript, explained

    What concatenation is

    Concatenation means joining strings end to end to make a longer string. In TypeScript, the + operator does it when its operands are strings:

    let message = "I love coding. " + "It is fun!";
    console.log(message);
    
    I love coding. It is fun!
    

    The result is a new string. The two originals are not changed. Notice the space at the end of "I love coding. ": the operator adds nothing of its own, so every character in the result comes from one of the pieces.

    Why join strings at all

    Joining two literals, as above, is pointless, because you could write the whole sentence in one string. Concatenation earns its place when some of the pieces are variables, so the message adapts to the data:

    let favoriteAnimal = "parrot";
    let message = "My favorite animal is the " + favoriteAnimal;
    console.log(message);
    
    My favorite animal is the parrot
    

    Change favoriteAnimal to "dog" and the sentence changes with it. The text inside quotes is fixed, while a variable name outside the quotes is replaced by its value. This is the most common beginner confusion: "favoriteAnimal" in quotes is the literal word, and favoriteAnimal without quotes is the variable.

    Joining more than two pieces

    Put a + between every pair of pieces. They are joined from left to right:

    let animal = "parrot";
    let owner = "Andy";
    console.log(owner + ", that's a nice " + animal + " you got there!");
    
    Andy, that's a nice parrot you got there!
    

    Spaces and punctuation are part of the strings you write, so you have to supply them. Forget the spaces and the words run together:

    console.log(owner + "that's a nice" + animal);
    
    Andythat's a niceparrot
    

    Numbers and + together

    When one side of + is a string and the other is a number, the number is converted to text and joined. The order of operations then matters, because + works left to right:

    let years = 3;
    console.log("Age: " + years + 1);
    console.log("Age: " + (years + 1));
    console.log(years + 1 + " is the age");
    
    Age: 31
    Age: 4
    4 is the age
    

    In the first line, "Age: " + years is already a string, so the 1 is joined as text and you get 31. Parentheses make the addition happen first. In the third line, years + 1 is added as numbers before the string appears.

    Building a string step by step

    You can also grow a variable by reassigning it, with += as a shorthand:

    let greeting = "Hello";
    greeting = greeting + ", world";
    greeting += "!";
    console.log(greeting);
    
    Hello, world!
    

    TypeScript also has template literals, written with backticks and ${...}, which put values directly into a string. This lesson practises +, which is the basic tool. For comparison, these two lines print the same text:

    console.log(`${owner} has a ${favoriteAnimal}`);
    console.log(owner + " has a " + favoriteAnimal);
    
    Andy has a parrot
    Andy has a parrot
    

    Template literals tend to read better when a sentence has many variables, because the text stays in one piece. The lesson asks for +, so use that here. Both approaches produce an ordinary string that you can print, store or join further.

    The lesson's exercise

    The starter code declares name and language. The job is to print one sentence built from four pieces: "My name is ", the value of name, " and I code in " and the value of language.

    1. Start a console.log call and put the first string inside it. It ends with a space so the name does not stick to the word "is".
    2. Add + name. The variable goes outside quotes, so its value is used.
    3. Add the middle string with a space on both sides, so the name and the following words are separated.
    4. Add + language to finish the sentence.
    let name: string = "John Doe";
    let language: string = "Python";
    
    console.log("My name is " + name + " and I code in " + language);
    

    This prints My name is John Doe and I code in Python. Change either variable and the sentence follows.

    Common mistakes

    • Putting a variable name in quotes. "I code in " + "language" prints I code in language, the literal word. Without the quotes, "I code in " + language prints I code in Python.
    • Forgetting a +. Writing "I code in " language is a syntax error, and the compiler reports ',' expected.
    • Forgetting spaces. The code runs, but the output is hard to read. Check the edges of each string.

    Key takeaways

    • + joins strings into a new string and adds nothing between them.
    • Text in quotes is fixed. A variable outside quotes contributes its value.
    • You supply the spaces and punctuation yourself.
    • A string followed by + and a number joins text, so use parentheses when you want the arithmetic first.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free