Course contentsShow
← JavaScript
Lesson 150 of 2,7235. StringsFree lesson

Multi-line Strings with Template Literals

Write naturally formatted multi-line strings without escape sequences.

Multi-line Strings with Template Literals

You'll learn how template literals make writing multi-line strings natural and readable, without awkward escape sequences.

The Problem with Traditional Strings

Remember how we used backslashes at the end of lines to create multi-line strings? That approach works, but it's clunky and error-prone. You had to carefully place \ at each line break, and the string itself didn't visually match what you'd get.

The Template Literal Solution

Template literals (using backticks) handle multi-line strings effortlessly. Just press Enter and keep typing—the line breaks are preserved exactly as you write them:

const poem = `Roses are red,
Violets are blue,
JavaScript is great,
And so are you!`;

console.log(poem);
// Output:
// Roses are red,
// Violets are blue,
// JavaScript is great,
// And so are you!

What You See Is What You Get

Unlike the backslash method, template literals preserve your formatting exactly. Every line break, space, and indentation you type becomes part of the string:

const message = `Hello,

This is a message
    with intentional spacing
        and line breaks.`;

Think of it like writing on paper—you just write naturally, and JavaScript respects your formatting.

No Special Characters Needed

No more \n for newlines or \ at line ends. The actual Enter key in your code creates the line break in the string. This makes your code far more readable, especially for longer text blocks.

Key Takeaway: Template literals let you write multi-line strings naturally by simply pressing Enter—line breaks are automatically preserved without any escape sequences or backslash tricks.