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.
- Start a
console.logcall and put the first string inside it. It ends with a space so the name does not stick to the word "is". - Add
+ name. The variable goes outside quotes, so its value is used. - Add the middle string with a space on both sides, so the name and the following words are separated.
- Add
+ languageto 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"printsI code in language, the literal word. Without the quotes,"I code in " + languageprintsI code in Python. - Forgetting a
+. Writing"I code in " languageis 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.