es6字符串
模板字符串
es6新增的字符串也被称为模板字符串
- ES5 中我们表示字符串的时候使⽤ ’ ’ 或者 " "。
- 在 ES6 中,我们还有⼀个东⻄可以表示字符串,就是 ``(反引号)。
let str = `hello world`
console.log(typeof str) // string
单引号和双引号的区别
- 反引号可以换⾏书写
let str = `
hello world
`
console.log(str) // 是可以使⽤的
- 反引号可以直接在字符串⾥⾯拼接变量
模板字符串在 `` ⾥⾯使用 ${} ⽤来书写变量的位置
// 模版字符串拼接变量
let num = 100 let str = `hello${num}world${num}`
console.log(str) // hello100world100
- 普通的字符串,一般情况下,只能放在一行处理
// 这个单引号或者双引号不能换⾏,换⾏就会报错了
let str = 'hello world'
// 下⾯这个就报错了
let str2 = 'hello
world'
- 进行字符串拼接,通过+号进行拼接
function showSelf({name, age = 40, sex = "男"}){
alert("我的名字叫" + name + ",今年" + age + "岁,性别是" + sex); }
来源:CSDN
作者:永夜的幻语
链接:https://blog.csdn.net/weixin_46397892/article/details/104647027