Beginner · 9 min
Template literals
Build readable messages with embedded values and expressions.
Embed a value
Template literals use backticks. Put an expression inside a dollar sign followed by an expression in braces to insert its value into the string.
const name = "Ada";
const count = 3;
console.log(`Hello, ${name}. Items: ${count}`);Output
Hello, Ada. Items: 3
Insert a calculation
An embedded expression can calculate a result. Keep the calculation short so the message stays easy to read.
const price = 4;
const quantity = 3;
console.log(`Total: ${price * quantity}`);Output
Total: 12
Put it into practice
- Predict each result before running the example.
- Complete the coding task, then explain why your solution works.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Build a ticket message
Use a template literal to set message to "Tickets: 4" using count, then display message.
Wrap count in braces immediately after a dollar sign within the backticks.