JavaScript has several important but easily forgotten operators

Read Xinyue 2021-09-15 10:51:43

Optional chain 【?.】

Allows you to read the value of an object's deep-seated properties .

const obj = {
info: {
name: "duxin",
money: 1000
}
}
const testObj = obj?.info?.age;
if (testObj) {
console.log(testObj);
}
 Copy code 

Null merge operator 【??】

When the left-hand operands are null perhaps undefined when , Returns the right-hand operand , Otherwise, return the left-hand operand .

const variable = undefined;
console.log(variable ?? "===");
 Copy code 

Logical null assignment 【??=】

The left operator is null perhaps undefined When , Give it a value .

Logical or assignment 【||=】

x ||= y x For false , The assignment is y

Logic and assignment 【&&=】

x &&= y , x It's true , The assignment is y

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15