create a template string in javascript

To create a template string in JavaScript, we use backticks (`) instead of single or double quotes. This allows us to embed expressions inside the string using ${} syntax, also known as String interpolation.

For example:

index.tsx
const name = 'John';
const age = 30;

const message = `My name is ${name} and I'm ${age} years old.`;

console.log(message);
// Output: My name is John and I'm 30 years old.
174 chars
8 lines

In the above code, we define a template string using backticks, and inside the string we use ${} to embed expressions. The values of name and age are interpolated into the final string.

Template strings are more readable and easier to maintain than concatenating strings using the + operator. They also support multi-line strings without the need for escape characters.

gistlibby LogSnag