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
This article example shares the specific code of ...
Table of contents 1. CentOS7+MySQL8.0, yum source...
Preface Managing routing is an essential feature ...
As a backend programmer, sometimes I have to tink...
Requirement: The page needs to display an image, ...
React Native is a cross-platform mobile applicati...
This article example shares the specific code of ...
This article example shares the specific code of ...
Preface: The Linux host is relatively easy to han...
The <input> tag The <input> tag is us...
Table of contents Install sakila Index Scan Sort ...
You can save this logo locally as a .rar file and...
We hope to insert the weather forecast into the w...
Today I received a disk warning notification from...
Table of contents 1. Installation 2. Introduction...