Languages
[Edit]
EN

JavaScript - string templates

0 points
Created by:
Aran-Busby
262

In this article, we would like to show you string templates in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

let username = 'Tom';
let template = `User: ${username} logged in.`;

console.log(template);

 

Practical example

In this example, we present step by step how to create template literals (string templates) in JavaScript.

1. Syntax

To define template literals, we need to use back-ticks (``).

let template = `Hello World!`;

2. Expression placeholders

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

// ONLINE-RUNNER:browser;

let x = 1;
let y = 2;

let template = `The result of x+y is ${x+y}.`;

console.log(template);

Output:

The result of x+y is 3.

References

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join