当前位置:网站首页>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 
边栏推荐
- How to improve deep learning performance?
- Kotlin是如何帮助你避免内存泄漏的?
- [graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32
- [FPGA]: MATLAB generates COE files
- [error prone points of C language] Part 4: detailed rules for storing structures in memory
- Getderivedstatefromprops lifecycle
- butterfly spreads
- Solution to using json.tojsonstring to display question marks in Chinese in Servlet
- 【嵌入式C基础】第9篇:C语言指针的基本用法
- 2020-12-07
猜你喜欢

Summary: golang's ide:vscode usage

Change the document type in endnode and import it in word

butterfly spreads

How to add PDF virtual printer in win11

Machine learning Basics - decision tree-12

Single option trading and cross trade

Leetcode daily question (2196. create binary tree from descriptions)
![[June 28 event preview] low code Summit](/img/ba/2f35306ed2fb0ece14d704256d8e60.png)
[June 28 event preview] low code Summit

为什么说Crypto游戏正在改变游戏产业?

How much do you know about JVM memory management
随机推荐
Protective bearish strategy
Black cat takes you to learn UFS agreement part 2: Interpretation of UFS related terms
[matlab] IIR filter
[error prone points of C language] Part 4: detailed rules for storing structures in memory
Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
Databinding+livedata can easily realize skin changing without restart
[embedded explanation] key scanning based on finite state machine and stm32
我抄底了被清算的NFT,却被OpenSea上了锁
Fast classification of array.group() in ES6
[FPGA]: MATLAB generates COE files
Form for real-time custom verification
10、 Kubernetes scheduling principle
Analysis of Andriod low on memory printing principle
【嵌入式C基础】第7篇:C语言流程控制详讲
Getderivedstatefromprops lifecycle
Protobuf data exchange format
Machine learning Basics - decision tree-12
QT signal and slot mechanism (detailed)
BiliBili Yang Zhou: above efficiency, efficient delivery
LeetCode每日一题(2196. Create Binary Tree From Descriptions)