String Interpolation Basics
Embed expressions inside template literals.
What Are Template Literals
Template literals are a way to create strings using the backtick character instead of quotes. They let you embed expressions directly inside a string without manual concatenation.
A placeholder is written with a dollar sign followed by curly braces wrapping an expression. JavaScript evaluates the expression and inserts its value into the string.
const name = "Alice"
const greeting = "Hello, " + name + "!"
console.log(greeting)Interpolation Syntax
Inside a template literal you place an expression between an opening dollar-brace and a closing brace. The expression is evaluated and converted to a string.
The example below uses plain concatenation to produce the same result you would get from interpolation, so the idea is clear without showing the literal syntax.
const product = "Book"
const price = 12
const line = product + " costs " + price + " dollars"
console.log(line)All lessons in this course
- String Interpolation Basics
- Multiline Strings
- Tagged Template Functions
- Practical Tagged Template Use Cases