Summary of common operators and operators in javascript
Uncommon operators and operators in jsNullish coalescing operator: ?? When the left operand is null or undefined, it returns the right operand, otherwise it returns the left operand. null ?? 'huli' // huli undefined ?? 'huli' // undefined '' ?? 'huli' // '' [] ?? 'huli' // [] ({}) ?? 'huli' // {} NaN ?? 'huli' // NaN false ?? 'huli' // false 0 ?? 'huli' // 0 Logical null assignment: ??= The logical null assignment operator (x ??= y) assigns a value to x only if it is nullish (null or undefined). const a = { duration: 50 }; a.duration ??= 10; console.log(a.duration); // expected output: 50 a.speed ??= 25; console.log(a.speed); // expected output: 25 Logical OR: || If the existence is true, then it is true, , whichever is the previous const a = 3; const b = -2; console.log(a > 0 || b > 0); // true
Logical or assignment: ||= If yes, return; if no, assign value 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" Logical AND: && If both exist, it is true, whichever is the latter. const a = 3; const b = -2; console.log(a > 0 && b > 0); // expected output: false Logical AND assignment: &&= Assign if exists let a = 1; let b = 0; a &&= 2; console.log(a); // expected output: 2 b &&= 2; console.log(b); // expected output: 0 Optional chaining operator: ?. The optional chaining operator ( ?. ) allows reading the value of a property that is deep in a chain of connected objects without having to explicitly verify that each reference in the chain is valid. The ?. operator functions similarly to the . chaining operator, except that instead of causing an error if the reference is nullish (null or undefined), the expression short-circuits to return undefined. When used with a function call, if the given function does not exist, undefined is returned. 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 SummarizeThis is the end of this article about uncommon operators and operators in js. For more relevant js operators and operators, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of effective user groups and initial user groups in Linux
>>: Detailed tutorial on installing different (two) versions of MySQL database on Windows
Set Tomcat to automatically start the service: I ...
1. Unzip the zip package to the installation dire...
Table of contents Preface Installation and Usage ...
MyISAM storage engine The MyISAM storage engine i...
In CentOS7, when we install MySQL, MariaDB will b...
1. MySql Architecture Before introducing the stor...
There are many commands used in the system, so ho...
I just saw a post titled "Flow Theory and Des...
No matter how wonderful your personal website is,...
introduction Most people who have used databases ...
As shown below: Mainly execute authorization comm...
All previous projects were deployed in the Window...
Preface: In the MySQL system, there are many diff...
Table of contents Hbase installation and configur...
Table of contents Ideas Request interception Resp...