Template Literal Syntax
What you'll learn: How to create strings using backticks that allow embedded expressions and natural multi-line formatting.
A Modern Way to Build Strings
Up until now, you've created strings with single or double quotes. JavaScript offers a third option: template literals, written with backticks (`). These provide two powerful features: they naturally handle multi-line text, and they let you embed expressions directly inside the string.
The Backtick Character
Template literals use the backtick character (`), typically found on the same key as the tilde (~) on your keyboard:
let message = `Hello, world!`;
This creates a string just like single or double quotes would.
Multi-line Made Simple
Unlike regular strings that require \n escape sequences or backslash line continuation, template literals preserve line breaks naturally:
let poem = `Roses are red,
Violets are blue,
JavaScript is fun,
And so are you!`;
console.log(poem);
// Prints exactly as written, with line breaks intact
The string includes the actual newline characters where you pressed Enter—no escape sequences needed.
Think of It Like a Blueprint
Imagine template literals as blueprints where you can mark spots that will be filled in later. The backticks establish the boundary, and everything inside—including line breaks and spaces—is preserved exactly as you type it.
let address = `123 Main Street
Apartment 4B
New York, NY 10001`;
Key Takeaway: Template literals (created with backticks) are a modern string format that automatically preserves line breaks and formatting, making multi-line strings clean and readable without escape sequences.