EN
JavaScript - string templates
0 points
In this article, we would like to show you string templates in JavaScript.
Quick solution:
xxxxxxxxxx
1
let username = 'Tom';
2
let template = `User: ${username} logged in.`;
3
4
console.log(template);
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 (``
).
xxxxxxxxxx
1
let template = `Hello World!`;
2. Expression placeholders
Template literals can contain placeholders indicated by the dollar sign and curly braces (${expression}
).
xxxxxxxxxx
1
let x = 1;
2
let y = 2;
3
4
let template = `The result of x+y is ${x+y}.`;
5
6
console.log(template);
Output:
xxxxxxxxxx
1
The result of x+y is 3.