Categories
JavaScript Answers

How to format a JavaScript string using placeholders and an object of substitutions?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *