Template literals let you embed variables and expressions directly inside strings. If you’ve used Python’s f-strings, this will feel instantly familiar.
Copy
const name = "Sarah";const age = 28;const message = `Hello, ${name}! You are ${age} years old.`;console.log(message); // "Hello, Sarah! You are 28 years old."
The key difference from regular strings: template literals use backticks (`), not quotes. Variables go inside ${}.
JavaScript
Python
Copy
const name = "Sarah";const role = "developer";const greeting = `Welcome, ${name}! You are a ${role}.`;console.log(greeting); // "Welcome, Sarah! You are a developer."
Copy
name = "Sarah"role = "developer"greeting = f"Welcome, {name}! You are a {role}."print(greeting) # "Welcome, Sarah! You are a developer."
Same concept, slightly different syntax. Backticks instead of f"...", and ${} instead of {}.
Template literals preserve line breaks. No need for \n characters or string concatenation.
Copy
const userName = "Sarah";const orderId = "ORD-2024-001";const email = `Hello ${userName},Your order has been confirmed.Order ID: ${orderId}Thanks for your purchase!`;console.log(email);
This is much cleaner than the alternative:
Copy
// ❌ Without template literals — messyconst email = "Hello " + userName + ",\n\n" + "Your order has been confirmed.\n" + "Order ID: " + orderId + "\n\n" + "Thanks for your purchase!";
Multi-line template literals preserve all whitespace, including indentation. If you indent the template literal inside a function, those spaces will appear in the output. Keep the content flush-left if spacing matters.
You’ll sometimes build HTML strings in JavaScript — especially before you learn React. Template literals make this readable.
Copy
const user = { name: "Sarah", email: "sarah@example.com", role: "Admin" };const html = `<div class="user-card"> <h2>${user.name}</h2> <p>${user.email}</p> <span class="badge">${user.role}</span></div>`;
Once you learn React in Section 7, you’ll use JSX instead of building HTML strings. But it’s useful to see this pattern first — it shows why template literals matter for web development.