1.字符串填充
let str='1'
str=str.padStart(5,0) //将字符串1 填充长度为5
console.log(str) //output: 00001
let str2='2'
str2.padEnd(5,0)
console.log(str2) // output: 10000
2.Spread运算符
早在2015年作为ES2015规范的一部分问世,但是它的一些用例经常被忽略
//Spread运算符的第一个用例是将项目从一个数组添加到另一个数组
const arr1=['one','two','three']
const arr2=[...arr1,'four']
console.log(arr2) // output['one','two','three','four']
// Rest参数 从Spread运算符开始,我们有了Rest参数,这有点像相反。该rest语法收集多个元素,并将它们“压缩”为单个元素。
const [one,...four]=['one','two','three','four']
console.log(one) //output: one
console.log(four) //output: ['two','three','four']
3.Array.prototype.includes
此功能使你可以查找数组是否包含项
const four=['one','two','three','four']
const found=four.includes('one')
console.log(found) //output: true
4.可选链接
可选的链接允许我们尝试检索嵌套在对象中的深层数据,而不必处理数据可能不存在的情况。
const obj={
name:'chan',
meta:{
age:23
}
}
const age=obj?.meta?.age
const sb=obj?.sb?.no
console.log(age) // output: 23
console.log(sb) // output: undefined