1. Introduction There are many operators in js, which have been mentioned in previous articles. For example: js integer operations: Using var foo = (12.4 / 4.13) | 0; // the result is 3 var bar = ~~(12.4 / 4.13); //The result is 3 There is another little trick, that is var eee="eee"; alert(!!eee) These are some operators. For details, please see 49 JavaScript tips and tricks. The js operator single vertical bar "|" A few more to add today: 2. Comma operatorlet x = 1; x = (x++, x); console.log(x); // expected output: 2 x = (2, 3); console.log(x); // expected output: 3 The comma operator evaluates the argument on the left before the argument on the right. The value of the rightmost argument is then returned. var a = 10, b = 20; function CommaTest(){ return a++, b++, 10; } var c = CommaTest(); alert(a); // returns 11 alert(b); // returns 21 alert(c); // returns 10 Now that we know the call function operators, let's take an example to illustrate how to handle their conflicts. alert(2*5, 2*4); // Output 10 The code above outputs 10, but if interpreted according to the principle of the comma operator, it should output 8. Why? Since the comma operator has the lowest precedence in alert((2*5, 2*4)); // returns 8 3. JavaScript Null Coalescing Operator (??) The This is different from the logical OR operator (||), which returns the right-hand operand if the left-hand operand is false. That is, if you use || to set default values for certain variables, you may encounter unexpected behavior. For example, when it is false (for example, '' or 0). See example below. let str = null||undefined let result = str??'haorooms blog' console.log(result)//haorooms blog const nullValue = null; const emptyText = ""; // Empty string, a false value, Boolean("") === false const someNumber = 42; const valA = nullValue ?? "the default value of valA"; const valB = emptyText ?? "The default value of valB"; const valC = someNumber ?? 0; console.log(valA); // "default value of valA" console.log(valB); // "" (Empty string is false, but not null or undefined) console.log(valC); // 42 4. javascript optional chaining operator (?.) The optional chaining operator ( Using the optional chaining operator ( ?. ) the browser will not complain! const demo = { name: 'haorooms', cat: { name: 'haorooms cat' } }; console.log(demo.dog?.name); // expected output: undefined console.log(demo.what?.()); // expected output: undefined Function call: let result = someOne.customMethod?.(); If you want to allow Optional chaining with expressions: let nestedProp = obj?.['prop' + 'Name']; Optional chaining to access arrays: let arrayItem = arr?.[42]; Short circuit calculation: let potentiallyNullObj = null; let x = 0; let prop = potentiallyNullObj?.[x++]; console.log(x); // x will not be incremented, and will still output 0 //When using optional chaining in an expression, if the left operand is null or undefined, the expression will not be evaluated let customer = { name: "haorooms", details: { age: 82 } }; let customerCity = customer?.city ?? "中国"; console.log(customerCity); // "China" This is the end of this article about the summary of uncommon js operation operators. For more relevant js operation operator content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed process of using Vscode combined with docker for development
>>: CSS implements a pop-up window effect with a mask layer that can be closed
There are many reasons for slow query speed, the ...
Table of contents 1. some 2. every 3. find 1. som...
1. Docker installation and startup yum install ep...
What is a tree in web design? Simply put, clicking...
Adding the attribute selected = "selected&quo...
Mine is: <!DOCTYPE html> Blog Garden: <!...
Preface: In MySQL, the system supports many chara...
This article shares the specific code of JS+AJAX ...
Preface A classmate is investigating MLSQL Stack&...
First, the structure inside the nginx container: ...
Table of contents After creating a container loca...
Table of contents 1. Introduction to SELinux 2. B...
If you want to become a Linux master, then master...
Table of contents 1. Constructors and prototypes ...
Before talking about CSS priority, we need to und...