1:键值打印
let num = {
age:'123',
name:'joe',
height:'196'
}
console.log({num}); //变量包装,可以同时打印出变量名和变量值
*可以打印出属性名和值,便于在复杂的业务中找到打印结果
2.&& ?? ?.
//1.&&
let num = 6
//if(num > 5){console.log('执行')}
//简化后:
num > 5 && console.log('执行');
//2.??
let obj = {
value:null
}
let objs = obj.value ?? '100'
//*null值判断符,当左边为null或者为undefined才返回右边的值
//3.?.
let street = user.address && user.address.street;
//简化后:
let street = user.address?.street;
//*?.也叫链式判断运算符,逐层判断左边是否为null或者undefined,如果是就不在继续往下运算,如果不是则返回右边的值
3.includes
let arrays = [8,9,10,11]
let num = 8
if(arrays.includes(num)){
console.log('ok');
}
//*如果数组中存在我想查询的值,则执行后面的步骤
4.字符串转为数字
let num = '44'
console.log(+num)
5.对象:object.hasOwn
let obj = {
name:'join',
age:'16'
}
console.log(Object.hasOwn(obj,'name'));
//*查询对象中是否有某个键,有则返回true
6..at
let arr = [1,2,3,4]
console.log(arr.at(-1));
console.log(arr.at(0))
//*查询数组中某一项下标的值,-1是最后一位,0是第一位