All posts
JavaScriptNovember 19, 20266 min read

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.

F
Fepiq Team
Fepiq

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.

What is a template literal?

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.

js
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.

The old way: joining strings with +

Before template literals, you joined text and variables with the plus sign. This is called concatenation, which just means joining things together.

js
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.

The new way: using ${ } to insert values

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.

js
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.

FeatureNormal string (quotes)Template literal (backticks)
Quotes usedSingle or doubleBackticks
Insert a variableJoin with +Use ${variable}
Multiple linesNeeds \nJust press Enter
Run math insideNeeds extra + and ()Use ${a + b}

Put math and code inside ${ }

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.

js
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.

Write text over many lines

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.

js
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.

Use template literals with functions

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.

js
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 Methods

Build HTML with template literals

A very common use is building pieces of a web page. Here we create a small card and put it on the page.

js
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.

Common mistakes to avoid

  • Using normal quotes: ${name} only works inside backticks. In normal quotes it prints the characters as they are.
  • Forgetting the curly braces: write ${name}, not $name.
  • Mixing up the backtick with the apostrophe.
  • Putting untrusted user text into innerHTML. It can be unsafe, so use textContent for plain text.

Quick recap

  1. Wrap your text in backticks.
  2. Use ${ } to insert variables or expressions.
  3. Press Enter for new lines.
  4. Use them to make your code shorter and easier to read.

Frequently asked questions

What are template literals in JavaScript?+

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.

What is the difference between template literals and normal strings?+

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.

How do I put a variable inside a string in JavaScript?+

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.

Can I use quotes inside a template literal?+

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.

Do template literals work in all browsers?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.