-
Null-value merge operator:??
If the left-hand operand is null or undefined, return the right-hand operand; otherwise, return the left-hand operand.
null ?? 'huli' // huli undefined ?? 'huli' // huli ' ' ?? 'huli' / /"[]????'huli' / / []({})??'huli' / / {} NaN ?? 'huli' // NaN false ?? 'huli' // false 0 ?? 'huli' / / 0 Copy the code
-
Logical null assignment:?? =
The logical null assignment operator (x?? = y) only if x is nullish (null or undefined).
const a = { duration: 50}; a.duration ?? =10; console.log(a.duration); // expected output: 50a.speed ?? =25; console.log(a.speed); // expected output: 25 Copy the code
-
Logic or: | |
If there is truth, it is true
const a = 3; const b = -2; console.log(a > 0 || b > 0); // true Copy the code
Can be the value of false
null
undefined
NaN
""
与' '
- 0
- false
-
Logic or assignment: | | =
Returns if it does, and assigns if it does not
const a = { duration: 50.title: ' ' }; a.duration ||= 10; console.log(a.duration); // expected output: 50 a.title ||= 'title is empty.'; console.log(a.title); // expected output: "title is empty" Copy the code
-
Logic and: &&
If both exist, it is true
const a = 3; const b = -2; console.log(a > 0 && b > 0); // expected output: false Copy the code
-
Logic and assignment: &&=
If it exists, it is assigned
let a = 1; let b = 0; a &&= 2; console.log(a); // expected output: 2 b &&= 2; console.log(b); // expected output: 0 Copy the code
-
Optional chain operators:? .
The optional chain operator (? .) allows you to read the value of a property located deep in the chain of connected objects without explicitly validating that each reference in the chain is valid. ? The. Operator functions like the. Chain operator, except that it does not cause errors when references are nullish (null or undefined), and the short-circuit returns undefined. When used with a function call, returns undefined if the given function does not exist.
const adventurer = { name: 'Alice'.cat: { name: 'Dinah'}};constdogName = adventurer.dog? .name;console.log(dogName); // expected output: undefined console.log(adventurer.someNonExistentMethod? . ());// expected output: undefined Copy the code