How to insert variables in JavaScript string? | Example code

How to insert variables in JavaScript string?

Using Template literals you can put variables inside JavaScript strings.

Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}).

Note: from 2015 onwards, just use backticks for templating. Above code that it is a backtick, not a quote.

Complete HTML example code:

Output:

Do comment if you have suggestions or doubts on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.