Nodejs by Example: Template-String

模板字符串可以让你更方便地创建包含变量或表达式的字符串。

可以通过反引号(`)来创建模板字符串,而不是单引号(‘)或双引号(“)

在模板字符串中,你可以使用 ${}

插值来插入变量或表达式。

在模板字符串中插入表达式

模板字符串还可以用来创建多行字符串

也可以用来创建html标签

console.log(`Hello, Tom!`); 


let name = 'John';
console.log(`Hello, ${name}!`); 

let a = 5;
let b = 10;
console.log(`The sum of a and b is ${a + b}.`); 

console.log(`This is a string
spanning multiple
lines`);

let title = 'Welcome to my website';
let body = 'Some text about my website.';

let html = `
    <h1>${title}</h1>
    <p>${body}</p>
`;
console.log(html);