1. Introduction
Template literals, introduced in ES6, are string literals delimited by backticks (`) instead of single or double quotes. They allow embedded expressions through interpolation, support multi-line strings without special escape characters, and can be customized further with tagged templates.
Cricket analogy: Template literals are like a scorecard printed with backticks instead of quotes, letting you drop live values like ${batsman} scored ${runs} directly into the sentence and even span multiple lines without special line-break codes.
2. Syntax
const name = "Ava";
const greeting = `Hello, ${name}!`;
const multiline = `Line 1
Line 2`;
function tag(strings, ...values) {
return strings.raw.join('|') + values.join(',');
}
tag`Value: ${1} and ${2}`;3. Explanation
Inside a template literal, any expression placed inside ${ ... } is evaluated and converted to a string, then inserted at that position — this is called interpolation and it replaces older, more error-prone string concatenation with +. Because backtick strings preserve literal newlines, you can write multi-line strings directly without \n escape sequences or string concatenation across lines.
Cricket analogy: Interpolation lets you write ${player} hit ${sixes} sixes instead of stitching 'player' + ' hit ' + sixes + ' sixes' with plus signs, and the backtick string keeps line breaks between overs literal, no \n needed.
A tagged template is a function call written as functionNametemplate. The function receives the literal string pieces (as an array, with a .raw property containing the unescaped source text) as its first argument, followed by the evaluated interpolated values as additional arguments, enabling custom processing such as sanitization or localization.
Cricket analogy: A tagged template is like handing a commentator the raw phrase pieces 'Over ' and ' bowled by ' separately along with the interpolated player name, so the commentator can translate or censor before announcing it.
Gotcha: ${ } only evaluates JavaScript expressions, not statements — you can call functions and use ternaries inside it (e.g. ${isAdmin ? 'Admin' : 'User'}), but you cannot put an if statement or a variable declaration inside the braces. Also, template literals are still just strings — ${a} == a is true or false depending only on type coercion rules, since the interpolated result is always converted to a string first.
4. Example
const name = "Ava";
const age = 28;
console.log(`Hello, my name is ${name} and I am ${age} years old.`);
const a = 5, b = 10;
console.log(`The sum of ${a} and ${b} is ${a + b}.`);
const multiline = `Line 1
Line 2`;
console.log(multiline);
function tag(strings, ...values) {
return strings.raw.join('|') + ' -- ' + values.join(',');
}
console.log(tag`Value: ${a} and ${b}`);5. Output
Hello, my name is Ava and I am 28 years old.
The sum of 5 and 10 is 15.
Line 1
Line 2
Value: | and | -- 5,106. Key Takeaways
- Template literals use backticks (`) instead of quotes and support ${expression} interpolation.
- Interpolated expressions are evaluated and converted to strings automatically, replacing manual + concatenation.
- Template literals preserve real line breaks, enabling multi-line strings without \n.
- ${ } only accepts expressions, not statements like if or variable declarations.
- Tagged templates (tag
...) let a function customize how a template literal's pieces and values are combined.
Practice what you learned
1. Which character delimits a template literal in JavaScript?
2. What is the output of `The sum of ${5} and ${10} is ${5 + 10}.`?
3. Can you place a statement, such as an if block, directly inside ${ } in a template literal?
4. How do template literals handle line breaks written directly inside the backticks?
5. In a tagged template function tag(strings, ...values), what does the 'strings' parameter represent?
Was this page helpful?
You May Also Like
Operators in JavaScript
Survey JavaScript's arithmetic, comparison, logical, and ternary operators and how they behave with type coercion.
Variables in JavaScript (var, let, const)
Learn how var, let, and const differ in scope, hoisting, and reassignment when declaring variables in JavaScript.
Console and Debugging in JavaScript
Learn the core console methods and debugging techniques for inspecting and troubleshooting JavaScript code.
Functions in JavaScript
Learn how to declare, invoke, and understand the hoisting behavior of JavaScript functions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics