当前位置:网站首页>JS中? ?和??=和?.和 ||的区别
JS中? ?和??=和?.和 ||的区别
2022-07-30 23:09:00 【asdfsdgfsdgfa】
undefined和null是两个比较特殊的数据类型,是不能用点操作符去访问属性的,否则将会报错
let a;
console.log(a?.name); //undefined
console.log(a.name); //报错 
let obj = {};
console.log(obj.name); //undefined
console.log(obj?.name?.a); //undefined
console.log(obj.name.a); //报错
?? 与 || 的区别
相同点:
?? 和 || 的用法相同,都是前后是值,中间用符号连接,根据前面的值来判断最终是返回前面的值还是后面的值。
A ?? B
A || B
不同点:
判断的方法不同:
使用 ?? 时,只有A为 null 或者 undefined 时才会返回 B;
使用 || 时,A会先转化为布尔值判断,为true时返回A , false 返回B
// ??
console.log(undefined ?? 2); // 2
console.log(null ?? 2); // 2
console.log(0 ?? 2); // 0
console.log("" ?? 2); // ''
console.log(true ?? 2); // true
console.log(false ?? 2); // false
// ||
console.log(undefined || 2); // 2
console.log(null || 2); // 2
console.log(0 || 2); // 2
console.log("" || 2); // 2
console.log(true || 2); // true
console.log(false || 2); // 2空值合并操作符 ??
只有当左侧为null 或者undefined 时,才会返回右侧的值
可选链操作符 ?.
?. 允许读取连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。
?. 操作符的功能类似于. 链操作符,不同之处在于,在引用为空,即 null 或者 undefined 的情况下不会引起错误,该表达式短路返回值。
const obj = { a: { b: [{ name: "obj" }] } };
// 原本的写法
console.log(obj && obj.a && obj.a.b.length && obj.a.b[0].name); //obj
// 可选链写法
console.log(obj?.a?.b?.[0]?.name); // obj
console.log(obj?.b?.c?.d); // undefined?.可以和 ?? 运算符结合使用
const obj = { a: { name: "obj" } };
console.log(obj?.a?.b ?? "hello world"); // hello world空值赋值运算符(??=)
当??=左侧的值为null、undefined的时候,才会将右侧变量的值赋值给左侧变量.其他所有值都不会进行赋值
let a = "你好";
let b = null;
let c = undefined;
let d = 0;
let e = "";
let f = true;
let g = false;
console.log((b ??= a)); // 你好
console.log((c ??= a)); // 你好
console.log((d ??= a)); // 0
console.log((e ??= a)); // ''
console.log((f ??= a)); // true
console.log((g ??= a)); // false
console.log(b); // 你好
console.log(c); // 你好
console.log(d); // 0
console.log(e); // ''
console.log(f); // true
console.log(g); // false边栏推荐
- Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
- Apache Doris系列之:深入认识实时分析型数据库Apache Doris
- 力扣题(3)—— 无重复字符的最长子串
- Summary of BFS questions
- Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
- 基于 Docker Compose 的 Nacos(MySQL 持久化)的搭建
- mysql跨库关联查询(dblink)
- mysql锁机制
- Flex布局使用
- 正则表达式语法及使用
猜你喜欢

打动中产精英群体,全新红旗H5用产品力跑赢需求

Chapter 8 Intermediate Shell Tools II

vulnhub靶机AI-Web-1.0渗透笔记

Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略

ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c

Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan

Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)

Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation

Computer shortcut icon whitening solution

EasyExcel综合课程实战
随机推荐
PS Basic Learning (1)
2022.7.27
The problem of sticky packets in tcp protocol transmission
matlab标量场作图
语言代码表
Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
详解操作符
ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
# Dasctf 7月赋能赛 WP
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 4 DHKLN
go版本升级
grub 学习
Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略
“蔚来杯“2022牛客暑期多校训练营4 DHKLN
2022.7.28
el-upload添加请求头
一文详解:SRv6 Policy模型、算路及引流
A detailed explanation: SRv6 Policy model, calculation and drainage
2022.7.30
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list