A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-template-literals/ below:

JavaScript Template Literals - GeeksforGeeks

JavaScript Template Literals

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 with Expressions

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}`);

Output
The sum of 5 and 10 is 15
Multi-line Strings with Template Literals

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);

Output
This is a multi-line
string that spans across
several lines.
Tagged Template Literals

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}!`);
Escaping Backticks and Dollar Signs

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);

Output
This is a backtick: ` and this is a dollar sign: $
Use Cases-JavaScript Template Literals 1. Multi-line Strings

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);

Output
Roses are red,
Violets are blue,
JavaScript is awesome,
And so are you!
2. Dynamic Expressions

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); 

Output
Sum of 5 and 10 is 15.
3. Tagged Templates

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);
4. HTML Template

Template literals build HTML strings dynamically. This example creates an h1 element.

JavaScript
const title = "Welcome";
const html = `<h1>${title}</h1>`;
console.log(html); 
5. Conditionals in Templates

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);
6. Loops with Templates

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);

Output
Items: 
- apple,
- banana,
- cherry
7. Embedding Functions

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