Sometimes, we want to format a JavaScript string using placeholders and an object of substitutions.
In this article, we’ll look at how to format a JavaScript string using placeholders and an object of substitutions.
How to format a JavaScript string using placeholders and an object of substitutions?
To format a JavaScript string using placeholders and an object of substitutions, we use template literals.
For instance, we write
const str = `My name is ${replacements.name} and my age is ${replacements.age}.`;
to interpolate the value of replacements.name
and replacements.age
into the string.
Conclusion
To format a JavaScript string using placeholders and an object of substitutions, we use template literals.