ES6总结 第二节 模板字符串的使用

202 阅读1分钟

es5 字符串拼接方式

let name = 'jack'
let age = 30

let str = 'My name is ' + name + " and my age is " + age   // 原有的字符串拼接方式
console.log(str)
// 输出 My name is jack and my age is 30 

es6 字符串拼接方式(更简单)

let name = 'jack'
let age = 30
let str = `My name is ${name} and my age is ${age}`
console.log(str)
// 通过Tab键上的反引号`包裹字符串,然后通过${变量名}的方式将之前定义的变量插入到字符串中
// 输出 My name is jack and my age is 30