※ 模板字串(template literal):
- 使用「``」作為語法,作為字串拼接。
- 呈現多行內容。
const multiLineString = `This is a string
that spans multiple
lines.`
console.log(multiLineString)
/*
This is a string
that spans multiple
lines.
*/
- 可使用${var}嵌套變數和任意表達式。
const a = 5
const b = 10
const result = `The sum of ${a} and ${b} is ${a + b}.`
console.log(result) // The sum of 5 and 10 is 15.
- 調用函數
function greet(name) {
return `Hello, ${name}`
}
const message = `${greet('Bob')}! How are you?`
console.log(message) // Hello, Bob! How are you?