|| 逻辑或
当左侧为false,返回右侧的值
当左侧为true,返回左侧的值
console.log('' || 1) //1
console.log(1 || '') //1
console.log(null || 1) //1
console.log('' || 0) //0
?? (空值合并操作符 | 逻辑操作符)
只有当左侧为null和undefined时,才会返回右侧的数
console.log(null ?? '右侧返回值') //右侧返回值
console.log(undefined ?? '右侧返回值') //右侧返回值
console.log('string' ?? '右侧返回值') //string
console.log(1 ?? '右侧返回值') //1
console.log(false ?? '右侧返回值') // false
?. (可选链接操作符)
const _p = {
name: 'pName',
child: {
name: 'childName'
}
}
console.log(_p.child?.name) //childName
console.log(_p.child.name?.age) //undefined
!! (0、-0、null、""、false、undefined 或者 NaN转化为false,其他为true)
console.log(!!0) //false
console.log(!!-0) //false
console.log(!!null) //false
console.log(!!'') //false
console.log(!!false) //false
console.log(!!undefined) //false
console.log(!!NaN) //false
console.log(!!1)//true
console.log(!!'string')//true
~~ (把表达式强行转为int)
console.log(~~5) //5
console.log(~~'5') //5
console.log(~~null) //0
console.log(~~undefined) //0
console.log(~~true) //1
console.log(~~false) //0
+ (把表达式强行转为int)
console.log(+5) //5
console.log(+'5') //5
console.log(+null) //0
console.log(+undefined) //NaN
console.log(+true) //1
console.log(+false) //0