JavaScript Template Literals Explained for Beginners
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
What are JavaScript template literals? They are a way to write text (called a string) using backticks instead of quotes. Inside them you can drop in variables with ${ } and write text over many lines. In this guide you will learn how they work, step by step, with small examples you can copy and run.
You do not need any experience. We explain every new word the first time we use it. To run the examples, open your browser, press F12, and click the Console tab. Then paste the code and press Enter.
A string is a piece of text in your code, like "Hello". A template literal is a newer kind of string. It uses the backtick character instead of a single or double quote. The backtick key is usually at the top left of your keyboard, below Esc.
const greeting = `Hello, world!`;
console.log(greeting);Line 1 creates a variable named greeting and stores the text inside the backticks. Line 2 prints it to the console. The result is the same as a normal string. The real power comes next.
Before template literals, you joined text and variables with the plus sign. This is called concatenation, which just means joining things together.
const name = "Sam";
const age = 25;
const message = "My name is " + name + " and I am " + age + " years old.";
console.log(message);This works, but it is easy to make mistakes. You must remember every quote, every plus sign, and every space. Notice the space at the end of "My name is ". If you forget it, the words stick together.
With a template literal, you write the whole sentence in one piece. Wherever you need a variable, you write ${variableName}. JavaScript replaces it with the real value. This is called string interpolation.
const name = "Sam";
const age = 25;
const message = `My name is ${name} and I am ${age} years old.`;
console.log(message);Lines 1 and 2 store a name and an age. Line 3 builds the sentence. ${name} becomes Sam and ${age} becomes 25. Line 4 prints: My name is Sam and I am 25 years old. It reads like a normal sentence, so it is much easier to understand.
| Feature | Normal string (quotes) | Template literal (backticks) |
|---|---|---|
| Quotes used | Single or double | Backticks |
| Insert a variable | Join with + | Use ${variable} |
| Multiple lines | Needs \n | Just press Enter |
| Run math inside | Needs extra + and () | Use ${a + b} |
The part inside ${ } is not limited to variable names. You can put any JavaScript expression there. An expression is a small piece of code that produces a value, like a calculation.
const price = 20;
const quantity = 3;
console.log(`Total: $${price * quantity}`);
console.log(`Is it expensive? ${price > 10 ? "Yes" : "No"}`);In line 3, JavaScript multiplies 20 by 3 and prints Total: $60. The first $ is just a normal dollar sign. The second $ with braces starts the insert. Line 4 uses a ternary, a short if/else. Because price is above 10, it prints Is it expensive? Yes.
With normal strings, you need the special code \n to start a new line. Template literals keep the line breaks exactly as you type them.
const poem = `Roses are red,
Violets are blue,
JavaScript is fun,
And so are you.`;
console.log(poem);The text is typed on four lines, and the console prints four lines. This is very handy when you build blocks of HTML or long messages.
You can also call a function inside ${ }. A function is a reusable set of instructions. Here we use a built-in string tool to make text uppercase.
const city = "paris";
console.log(`Welcome to ${city.toUpperCase()}!`);
function double(n) {
return n * 2;
}
console.log(`Double 4 is ${double(4)}`);Line 2 calls toUpperCase() on the city, so it prints Welcome to PARIS!. Lines 4 to 6 define a function named double. Line 7 calls it inside the template and prints Double 4 is 8. To learn more text tools, read our guide on JavaScript string methods.
Want to learn more ways to work with text? Read our beginner guide to JavaScript string methods.
Read: JavaScript String MethodsA very common use is building pieces of a web page. Here we create a small card and put it on the page.
const user = { name: "Ana", job: "Designer" };
const card = `
<div class="card">
<h2>${user.name}</h2>
<p>${user.job}</p>
</div>
`;
document.body.innerHTML = card;Line 1 makes an object with a name and a job. Lines 3 to 8 build the HTML text, inserting user.name and user.job. Line 10 puts the card on the page. Without template literals, this would need many plus signs and quotes.
Template literals are strings written with backticks. They let you insert variables with ${ } and write text over several lines. They were added to JavaScript in 2015 (ES6) and work in all modern browsers.
Normal strings use single or double quotes and need + to join values. Template literals use backticks, insert values with ${ }, and keep line breaks. The result is still a normal string.
Use a template literal. Write your text between backticks and put the variable name inside ${ }, like `Hello ${name}`. JavaScript swaps in the variable's value.
Yes. You can use single and double quotes freely inside backticks without escaping them. Only a backtick itself needs a backslash in front of it.
They work in every modern browser, including Chrome, Firefox, Safari and Edge. Only very old browsers such as Internet Explorer do not support them.
Learning to code and want help building a real website or app? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
PostgreSQL date and time for beginners: learn DATE, TIMESTAMP, NOW(), INTERVAL and filtering by date with simple, copy-paste examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.