Hello! Welcome back to our exploration of JavaScript's operators.
In our last lesson, we focused on arithmetic operators like +, *, and /, learning how they perform calculations and follow a specific order of precedence. We briefly touched on the special nature of the + operatorβhow it can add numbers but also do something entirely different with strings.
Today, we'll dive deep into that "something different." Our goal is to master two ways of combining strings: the traditional method using the + operator, known as concatenation, and a modern, far more powerful feature called template literals. This is a fundamental skill for creating dynamic text, which is at the heart of building applications with Next.js where you constantly display changing data to users.
The Traditional Way: String Concatenation
The most basic way to join strings is by using the + operator. This process is called concatenation.
Let's watch a short segment that defines and demonstrates this process. Given your preference for visual learning, the animations in this video should provide a clear mental model.
Working with Strings in JavaScript - Concatenation & Template Literals Explained
The video 'Working with Strings in JavaScript' by Future Fullstack introduces concatenation very clearly.
Watch the first two minutes of the video (00:00 to 02:56). Pay close attention to how strings are joined and, importantly, how spaces are managed, as this is a common point of confusion.
As you saw, you can join strings by simply "adding" them together. To create a full name from a first and last name, you might write:
let firstName = 'Jane';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName; // 'Jane Doe'
Notice that we had to explicitly add a string containing a single space (' ') to separate the names.
The Pitfall of Concatenation: Type Coercion
This approach works, but it can get messy and lead to bugs, especially when you mix strings and numbers. As we've discussed, JavaScript performs implicit type coercion. When the + operator sees a string on one side, it favors concatenation and converts the other value to a string.
Let's see a visual explanation of this behavior and the problems it can cause.
Working with Strings in JavaScript - Concatenation & Template Literals Explained
Let's return to the 'Working with Strings in JavaScript' video to see how JavaScript handles operations between strings and numbers.
Watch from 04:21 to 05:23 and then from 07:55 to 09:41. The first part explains the coercion rule, and the second part shows a very realistic example of how messy and error-prone building a complex sentence with concatenation can be.
The video highlighted two key issues:
- Readability: A long string built with many
+signs and variables is hard to read and write. It's easy to miss a space or a quote. - Unexpected Bugs:
'34' + 1results in the string'341', not the number35. This happens because the number1is coerced into a string and then concatenated.
This messiness and potential for error are exactly why modern JavaScript provides a better way.
A Modern Approach: Template Literals
Introduced in ES6, template literals (also called template strings) offer a cleaner, more intuitive way to create strings. They address all the shortcomings of concatenation.

Template literals are defined using backticks (`) instead of single (') or double (") quotes. Their superpower is the ability to embed expressions directly inside the string using the ${expression} syntax, a feature called interpolation.
Let's see them in action.
Master JavaScript Template Strings: The Ultimate Guide to ...
The article 'Master JavaScript Template Strings' on dev.to provides an excellent side-by-side comparison.
Read the sections 'The Old Way: String Concatenation' and 'The New Way: Template Strings'. This will give you a clear, text-based comparison of the two approaches.
Using template literals, the complex sentence from the video becomes much more readable:
// The messy concatenation way
// My name is Daniel Brand, I live in London, I am 34 and next year I will be 341.
// The clean template literal way
let message = `My name is ${firstName} ${lastName}, I live in ${city}, I am ${age} and next year I will be ${age + 1}.`;
// My name is Daniel Brand, I live in London, I am 34 and next year I will be 35.
Notice two amazing things here:
- The string is written just like a natural sentence.
- The calculation
age + 1works correctly inside the${}block before being converted to a string!
The Superpowers of Template Literals
Template literals are more than just a neater way to insert variables.
1. Easy Multi-line Strings
With traditional strings, creating text that spans multiple lines requires adding a newline character (\n) or breaking the string with +. Template literals respect whitespace, including line breaks, exactly as you type them.
The video 'JavaScript Template Literals' by Programming with Mosh offers a great demonstration of how template literals simplify multi-line strings.
Watch from 01:40 to 03:13. See how you can format text, like an email, exactly as you want it to appear.
2. Embedded Expressions
The ${...} syntax can hold any valid JavaScript expression. This includes arithmetic, function calls, and even other operators.

Let's see a practical demonstration that combines all these features.
Working with Strings in JavaScript - Concatenation & Template Literals Explained
We'll watch one last clip from the Future Fullstack video. It masterfully demonstrates building a complex string with calculations using both methods, making the benefits of template literals undeniable.
Watch from 11:31 to 15:12. Follow along as the instructor builds the same sentence using concatenation and then template literals. The difference in clarity and ease is the key takeaway.
Test your understanding!
You have the following variables:
let item = 'Coffee';
let quantity = 2;
let pricePerItem = 3;
Your goal is to log the following sentence to the console:"2 cups of Coffee will cost $6."
How would you create this sentence using:
- Traditional string concatenation?
- Template literals?
Show answer
-
String Concatenation:
console.log(quantity + " cups of " + item + " will cost $" + (quantity * pricePerItem) + ".");Note the need for parentheses around
quantity * pricePerItemto ensure the calculation happens before any potential string coercion. -
Template Literals:
console.log(`${quantity} cups of ${item} will cost $${quantity * pricePerItem}.`);This version is much cleaner and more intuitive to read. The calculation is handled naturally within the expression placeholder.
Practice Exercises
Now it's time to solidify your knowledge. These exercises from Launch School will guide you through applying both methods.
The basic building blocks of the JavaScript programming ...
First, let's practice basic concatenation.
Find 'Exercise 1' on the page. Complete the task of combining a first and last name into a full name using the '+' operator.
The basic building blocks of the JavaScript programming ...
Now, let's use the modern approach with template literals.
Find 'Exercise 6' on the page. Follow the instructions to construct a sentence using template literals and an embedded arithmetic expression.
Conclusion
Fantastic work! You've now learned how to build dynamic strings, a skill that's essential for almost any program you'll ever write.
Key Takeaways:
- String concatenation uses the
+operator to join strings, but it can be verbose and lead to bugs from implicit type coercion. - Template literals use backticks (
`) and are the modern, preferred way to create strings in JavaScript. - String interpolation (
${...}) allows you to embed variables and any valid JavaScript expression directly and readably into your strings. - Template literals make multi-line strings and embedded calculations simple and intuitive.
Next Up:
We've learned how to create and manipulate data. The next logical step is to learn how to compare it. In our next lesson, we will explore comparison operators like === and >. These are the tools that allow your programs to ask questions and make decisions, forming the foundation of all logic in your code.