Ad space (banner)
🟨JavaScript Lessons
Lesson 47 / 69

Multi-Line Strings (Template Literals)

In this lesson you'll learn how to write multi-line strings using template literals, so you can build messages in a readable way. This is for people searching "JavaScript template literal usage" or "JavaScript multi-line string."

A template literal, wrapped in backticks (` `), can include real line breaks directly, so you can write text that spans multiple lines in a readable way. It's handy for building things like the body of an email or a fragment of HTML. A huge advantage is that you can embed a variable's value directly inside the string using ${variable}.

The sample code embeds a variable inside a string using ${name}, while building a multi-line message that includes real line breaks. Doing the same thing with the traditional + string concatenation would require writing + and " " for every single line, making the code hard to read — but a template literal lets you write it naturally, exactly as it will look.

A common beginner stumbling block is confusing an ordinary single or double quote with a backtick. The backtick is a special symbol usually near the top-left of the keyboard, and without it, embedding a variable with ${} simply won't work. Another advantage inside a template literal is that you can use ordinary quote characters without escaping them.

Because it lets you assemble strings so cleanly, template literals are used as the standard in real-world code too. From email templates to assembling messages sent to an API, template literals are put to use in every situation involving multi-line text.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)