Master modern string manipulation, multi-line formatting, dynamic expression interpolation, and clean string composition using ES6 template literals.
Learn how variables act as labeled containers for data using const, let, and var to manage lexical scope and reassignment safely in modern applications.
A string is a sequence of characters used to represent text. In JavaScript, strings can be enclosed in single quotes ('...), double quotes ("..."), or ES6 backticks (...).
Building dynamic user greetings or UI components with traditional plus-sign concatenation ("Hello " + username + "! You have " + count + " items.") is clunky and prone to missing spaces. ES6 template literals make dynamic text interpolation clean and readable.
Wrap your string in backticks (...) and embed any valid JavaScript expression directly inside ${expression}:
const user = "Alex";
const unreadMessages = 4;
const role = "Admin";
// Clean template literal formatting
const notification = `Welcome back, ${user} (${role})! You have ${unreadMessages} unread messages.`;
console.log(notification);
// Output: "Welcome back, Alex (Admin)! You have 4 unread messages."
Template literals also support multiline formatting natively without needing newline \n escape characters:
const htmlCard = `
<div class="user-card">
<h2>${user}</h2>
<span class="badge">${role}</span>
</div>
`;
Inside ${}, JavaScript evaluates any expression before inserting the string result:
const itemPrice = 29.99;
const taxRate = 0.08;
const summary = `Total price: $${(itemPrice * (1 + taxRate)).toFixed(2)}`;
console.log(summary); // "Total price: $32.39"
${user} only evaluates inside backticks (...). Inside single or double quotes, "${user}" will print literally as text.\ `.Use template literals for all dynamic text creation. Next, let's explore numbers, arithmetic, and modulo operations in JavaScript!