有时候我们会遇到要处理多个字符串+常量/变量的情况比如需要输出“My name is” 如果用字符串拼接的写法,那么写法就如下
const name = "Kayn";console.log("My name is" + name);这样的缺点在于
"Hello " + firstName + " " + lastName + ", you are " + age + " years old.";\n:"Line1\nLine2\nLine3";于是就有了模板字符串(Template Literal),它是一种字符串字面量的新形式:
` 包裹。${} 语法。\n。
它的写法为const name = "Kayn";console.log(`My name is ${name}`);这样写的好处在于
${变量},看起来就像在自然语言中嵌入变量。`Hello ${firstName} ${lastName}, you are ${age} years old.`;\n。`Line1Line2Line3`;`Next year, you'll be ${age + 1} years old.`;