你知道?.这个符号吗?JavaScript中??和?. 的含义有啥区别

 

前言:

        你知道?.这个符号吗

比如有的对象中有children是个数组,有的对象的children是null ,比如有的对象中有children是个数组,有的对象的children是null

空值合并符号 ??

 只有当左侧为null 和undefined时,才会返回右侧的数

const too = null ?? 'default string'

// default string

?.       可选操作符

在引用为空 null 或者 undefined 的情况下不会引起错误,

const adventurer = {
    name :'abc',
    cat:{
        name:'maomi'
    }
}


const dogName = adventurer.dog?.name

console.log(dogName)  // unidefine

脑子又涨小知识了~

猜你喜欢

转载自blog.csdn.net/zhangxueyou2223/article/details/130953581
今日推荐