当前位置:网站首页>ES6 null merge operator (?)
ES6 null merge operator (?)
2022-07-28 13:17:00 【Pig Xiaoyong】
List of articles
Null merge operator (??)
When we get data from the back-end interface , We often encounter the lack of fields at the back end , The front end has to do short value compatibility , Prevent the back end from not giving fields
For example, when we need to obtain commodity prices , We will write :
const price = res.data?.price || ' No price '
But in fact, this approach is problematic , Obviously easy to see , If the price of a free item is 0 Words , We have || There will be a bug. And used to it || When writing , It's easy to overlook this potential problem . wait until bug When it came into being , We can only change it to :
const price = (res.data?.price === null || res.data?.price === undefined) ? ' No price ' : res.data?.price
Obviously the code is long , The readability is poor . Is there a better way to judge whether the field is empty , Yes , That is the null merge operator ??.
const price = res.data?.price ?? ' No price '
The code becomes much simpler and more comfortable !
You can see , Null merge operator (??) It's a logical operator , When the operands on the left are null perhaps undefined when , Returns its right-hand operand , Otherwise, return the left-hand operand .
In some business scenarios , Can replace ||, To avoid using || To set default values for some variables , You may encounter unexpected behavior .( for example ,’ ’ or 0)
const res={
data:{
price:0}}
const price=res.data?.price??'123'
console.log(price)
0
It is easy to ignore the judgment written normally 0 And empty string ’ ’
Use ?? Can accurately judge 
边栏推荐
- Analysis of Andriod low on memory printing principle
- Android engineers, how to use kotlin to provide productivity?
- Rust 从入门到精通01-简介
- Redis —— 基础篇
- Margin calculation
- Xampp Chinese tutorial guide
- A brief introduction to the for loop. Some of the code involves arrays
- Phpstudy steps to quickly build a website (teach you to build it by hand)
- Jetpack 全家桶之 LiveData 使用及源码篇
- Change password, confirm password verification antd
猜你喜欢

Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
![[graduation design] heart rate detection system based on single chip microcomputer - STM32 embedded Internet of things](/img/b4/06c822c52f5bb0045698b7107efb26.png)
[graduation design] heart rate detection system based on single chip microcomputer - STM32 embedded Internet of things
![[graduation design] oscilloscope design and Implementation Based on STM32 - single chip microcomputer Internet of things](/img/c7/87027ded6d9e522952827fae0fccbd.png)
[graduation design] oscilloscope design and Implementation Based on STM32 - single chip microcomputer Internet of things

Introduction to border border attribute

Code layered management of interface testing based on RF framework

How to add PDF virtual printer in win11

企业数字化本质

【嵌入式C基础】第4篇:运算符的使用

Analysis of Andriod low on memory printing principle

Unity—“合成大西瓜”小游戏笔记
随机推荐
Full disclosure! Huawei cloud distributed cloud native technology and Practice
Analysis of Andriod low on memory printing principle
.NET的求复杂类型集合的差集、交集、并集
机器学习基础-集成学习-13
什么是事务及数据库的优化方法
How to open the power saving mode of win11 system computer
【嵌入式C基础】第9篇:C语言指针的基本用法
Machine learning Basics - decision tree-12
[graduation design] oscilloscope design and Implementation Based on STM32 - single chip microcomputer Internet of things
[FPGA]: ISE generates MCS file and burning process
SSM框架网上书城全套
Unity—“合成大西瓜”小游戏笔记
CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
Li FuPan: application practice of kata safety container in ant group
[error prone points of C language] Part 4: detailed rules for storing structures in memory
Remove the plug-in of category in WordPress link
Change the document type in endnode and import it in word
Le transaction
What if win11 cannot recognize Ethernet
2020jenkins study notes