Template Literals and Tags

Introduction: Beyond String Concatenation

In our previous lessons, we spent a lot of time learning how to manage, protect, and move data. We used destructuring to unpack objects and optional chaining to safely navigate missing information. However, eventually, you will need to display that data to a user as a readable message. In the past, JavaScript developers had to build strings by joining pieces together with the plus sign (+). This process, called concatenation, often becomes messy and hard to read, especially when you have to manage spaces or multi-line text manually.

Modern JavaScript provides a much cleaner solution called template literals. These allow you to embed variables directly into strings and handle multiple lines of text with ease. Beyond simple formatting, JavaScript also offers a powerful feature called tagged templates. This allows you to run a string through a function before it is displayed, which is incredibly useful for tasks such as formatting currency or securing user input. In this lesson, we will explore how these tools make your code more readable and your data presentation more professional within the CodeSignal IDE.

Template Literal Basics

Multi-line Strings Made Easy

One of the most frustrating parts of older JavaScript syntax was creating strings that spanned multiple lines. You used to have to add a special newline character (\n) at the end of every line or concatenate several strings together. With template literals, JavaScript respects the actual line breaks you type inside the backticks.

Looking back at our previous code example, you can see that the summary variable was defined over two separate lines within the backticks. When we printed it to the console, the output maintained that exact same structure. This makes template literals perfect for building blocks of HTML or long text messages where the visual layout is important. You no longer need to worry about manual line breaks; you simply press Enter and keep typing.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal