[JS] 逻辑运算符(?? ?.)

数字开拓者说
• 阅读 320

记录一下空值合并操作符 ??(Nullish coalescing operator) 及 可选链操作符 ?. (Optional chaining)

// 空值合并操作符 ?? (Nullish coalescing operator)
// 只有当左侧为null和undefined时,才会返回右侧的数
const foo001 = null ?? 'default string';
console.log(foo001);
// expected output: "default string"

const foo002 = undefined ?? 'default string';
console.log(foo002);
// expected output: "default string"

const foo003 = 0 ?? 42;
console.log(foo003);
// expected output: 0

/*
注意:

?? 运算符不能与 AND 或 OR 运算符共用 
(来自 MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)

*/

// null || undefined ?? "foo"; // 抛出 SyntaxError
// true || undefined ?? "foo"; // 抛出 SyntaxError

// ↑ 上面两行的写法也会被eslint校验报错 '||' and '??' operations cannot be mixed without parentheses
// 但是,如果使用括号来显式表明运算优先级,是没有问题的:
(null || undefined) ?? "foo"; // 返回 "foo"

/*
此外:

与逻辑或操作符(||)不同,逻辑或操作符会在左侧操作数为假值(false)时返回右侧操作数。
比如为假值(例如,'' 或 0)时。
见下面的例子。  

*/
const foo004 = '' || 'default string';
console.log(foo004);
// expected output: "default string"

const foo005 = '' ?? 'default string';
console.log(foo005);
// expected output: ""

const foo006 = 0 || 42;
console.log(foo006);
// expected output: 42

const foo007 = 0 ?? 42;
console.log(foo007);
// expected output: 0

// 可选链操作符 ?. (Optional chaining)
// 可选链操作符 ?. 允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。
// "?."操作符的功能类似于 "."链式操作符,不同之处在于,如果引用的属性不存在时 (null 或者 undefined) ,不会引发错误,而是返回 undefined。
const adventurer = {
name: 'Alice',
cat: {

name: 'Dinah'

}
};

const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined

console.log(adventurer.someNonExistentMethod?.());
// expected output: undefined

完结。
同步更新到自己的语雀
https://www.yuque.com/dirackeeko/blog/guggdutsegxzqrgl

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
劳伦斯 劳伦斯
4年前
前端面试题自检 JS CSS 部分
JS类型JavaScript的简单数据类型Number,String,Boolean,Undefined,Null,Symboltypeof操作符的返回值numberstringbooleanundefinedobjectfunction
Easter79 Easter79
3年前
SpringBoot自定义序列化的使用方式
场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Stella981 Stella981
3年前
SpringBoot自定义序列化的使用方式
场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
数字开拓者说
数字开拓者说
Lv1
离别家乡岁月多,近来人事半消磨。
文章
5
粉丝
0
获赞
0