Last Updated : 31 Jul, 2025
Template literals are string literals that allow embedded expressions (variables) into your code. They are enclosed by backticks (`) instead of single (') or double (") quotes.
It was introduced in ES6, which provides a more flexible and readable way to work with strings. Unlike traditional string concatenation, template literals simplify the process of embedding expressions, multi-line strings, and variable interpolation.
Syntax
`string text ${expression} string text`
Basic Example
JavaScript
let a='GFG'
console.log(`hello ${a}`)
String interpolation lets you insert not just variables but also expressions, like math operations or function results, directly into a string.
JavaScript
let x = 5;
let y = 10;
console.log(`The sum of ${x} and ${y} is ${x + y}`);
The sum of 5 and 10 is 15
Template literals allow you to create multi-line strings easily without needing escape characters like \n.
JavaScript
const s = `This is a multi-line
string that spans across
several lines.`;
console.log(s);
This is a multi-line string that spans across several lines.
Tagged template literals allow you to customize how the string is processed, providing more control over string creation.
JavaScript
function greet(strings, name) {
return `${strings[0]}${name.toUpperCase()}${strings[1]}`;
}
const name = 'gfg';
console.log(greet`Hello, ${name}!`);
In JavaScript, if you need to include backticks or dollar signs inside template literals, you can escape them using a backslash (\).
JavaScript
const s = `This is a backtick: \` and this is a dollar sign: \$`;
console.log(s);
This is a backtick: ` and this is a dollar sign: $
Template literals support multi-line strings without special characters. This example displays a simple poem.
JavaScript
const poem = `Roses are red,
Violets are blue,
JavaScript is awesome,
And so are you!`;
console.log(poem);
Roses are red, Violets are blue, JavaScript is awesome, And so are you!
Embedding arithmetic expressions within template literals. This example calculates the sum dynamically.
JavaScript
const a = 5, b = 10;
const result = `Sum of ${a} and ${b} is ${a + b}.`;
console.log(result);
Sum of 5 and 10 is 15.
Tags process template literals directly, modifying how strings and placeholders are combined.
JavaScript
function tag(strings, ...values) {
return strings.reduce((acc, str, i) => acc + str + (values[i] || ''), '');
}
const output = tag`Hello, ${"Saran"}!`;
console.log(output);
Template literals build HTML strings dynamically. This example creates an h1 element.
JavaScript
const title = "Welcome";
const html = `<h1>${title}</h1>`;
console.log(html);
Ternary operators evaluate conditions within template literals. This example displays a role based on a condition.
JavaScript
const isAdmin = true;
const userRole = `User role: ${isAdmin ? "Admin" : "Guest"}.`;
console.log(userRole);
Loops generate dynamic content within templates. This example creates a list of items.
JavaScript
const items = ["apple", "banana", "cherry"];
const list = `Items: ${items.map(item => `\n- ${item}`)}`;
console.log(list);
Items: - apple, - banana, - cherry
Template literals call functions directly. This example transforms text to uppercase.
JavaScript
const toUpper = str => str.toUpperCase();
const s = `Shouting: ${toUpper("hello")}`;
console.log(s);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4