In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the powerful operators in JavaScript, I hope you will learn something after reading this article, let's discuss it together!
One. Null merge operator
If you meet it for the first time and see two question marks, it is estimated that there are more question marks in your mind (children, do you have many question marks)
Two question marks? Its good name is the null value merge operator, and if the first parameter is not null/undefined, the first parameter is returned, otherwise the second parameter is returned.
Console.log (1? "www.shanzhzonglei.com"); / / 1console.log (false?? "www.shanzhzonglei.com"); / / falseconsole.log (null?? "www.shanzhzonglei.com"); / / www.shanzhzonglei.comconsole.log (undefined?? "www.shanzhzonglei.com"); / www.shanzhzonglei.com
Therefore, the second parameter is returned only if the first parameter is null/undefined.
Note that although the undefined undefined, empty object null, numeric value 0, empty number NaN, Boolean false, empty string''in JS are all false values,? The non-airlift operator only handles null/undefined.
It is different from the logic or operator (| |), which returns the right Operand when the left Operand is false. Such as when it is a false value (''or 0).
Console.log (1 | | 2); / / 1console.log ("| | 2); / / 2, = null assignment operator
Oh, now there are more than two question marks and there is an equal sign. Is the question getting more and more difficult?
= null assignment operator, which is assigned only if the value is null or undefined.
Const student = {age: 20}; student.age? = 18 console.log (student.age); / / 20student.name? = "shanguagua"; console.log (student.name); / / 'shanguagua'
It and the one on it? The null merge operator is related: X? = y is equivalent to x? (X = y), x = y executes only if x is null or undefined.
Let x = null;x? = 20 20let console.log (x); / / console.log (x); / / 3. Optional chain operator
Optional chain operator?. Allows you to read the value of a property deep in the chain of connected objects without explicitly verifying that each reference in the chain is valid. The operator implicitly checks whether the object's property is null or undefined, and the code is more elegant and concise.
Const obj = {name: "mountain quack", foo: {bar: {baz: 18, fun: () = > {}, school: {students: [{name: "shanguagua",},],}, say () {return "www.shanzhonglei.com";},}; console.log (obj?.foo?.bar?.baz) / / 18console.log (obj?.school?.students?. [0] ["name"]); / / shanguaguaconsole.log (obj?.say?. ()); / / www.shanzhonglei.com quad,?: ternary operator
It is also called the ternary operator. Well, this is very common.
For conditional expression b? X: y, first calculate condition b, and then judge. If the value of b is true, the value of x is calculated, and the result is the value of x; otherwise, the value of y is calculated, and the result is the value of y.
Console.log (false? 1: 2); / / 2console.log (true? 1: 2); / / 1 V, logic and (&) and logic or (| |)
Let's review it first:
Logic and (& &): when the first Operand is true, the second Operand will not be judged, because regardless of the second Operand, the final result must be true.
In the actual development, using a feature, the following operations can be achieved:
1. If a value is true, run a function
Function say () {console.log ("www.shanzhonglei.com");} let type = true;type & & say (); / / www.shanzhonglei.com
2. Judge a certain value
/ / if will be executed only if age is greater than 10 and less than 20 (age > 10 & & age < 20) {console.log (age);}
Logical or (| |): when the first Operand is false (that is, the false value of js), the second Operand will not be judged, because no matter what the second Operand is, the final result must be false.
In the actual development, using a feature, the following operations can be achieved:
1. Set the initial value for a variable
Let student = {name: "shanguagua",}; console.log (student.age | | "www.shanzhonglei.com"); / / www.shanzhonglei.com
2. Judge a certain value
/ / if age equals 10 or 20 or 30, execute if (age = 10 | | age = 20 | | age = 30) {console.log (age);} 6, bit operators & and |
The bit operator operates on a bitwise basis, & (and), | (or). Decimal places are discarded when using the bit operator, and we can use | 0 to round the number. You can also use & 1 to determine odd and even numbers.
In the actual development, using a feature, the following operations can be achieved:
1. Rounding
1.3 | (0-/ / print out 11.9) | 0; / print-1
2. Judge the odd and even numbers
Let num = 5 positives! (num & 1); / / trueworthy! (num% 2); / / truth 7, two-bit operator ~ ~
You can use the two-bit operator to replace the positive number's Math.floor () and the negative number's Math.ceil ().
The advantage of the double negative positioning operator is that it runs faster when performing the same operation. For positive numbers, the result of ~ ~ is the same as that of Math.floor (), and for negative numbers, it is the same as Math.ceil ().
Math.floor (5.2) = 5; / / true~~3.2 = 3; / / trueMath.ceil (- 6.6) =-6; / / true~~-4.5 =-6; / / truth 7, logical operator!
!, you can convert variables to boolean types, null, undefined, and empty string''are all true, and the rest are false. Generally speaking, there are several uses,!
7.1 use! Take the reverse let cat = false;console.log (! cat); / / true7.2 use! Make a type judgment
A method that determines that the variable an is not equal to null,undefined and''.
Var an if (a! = null & & typeof a! = undefined & & a! = "") {/ / a code executed only if there is content}
Equivalent to:
If (!! a) {/ / a code executed only if there is content.} 7.3 whether the two values are equal
Generally speaking, it is not equal to! =, because if it is not equal to! =, 0! = "" returns false, because the conversion of 0 and''into Boolean type in JS is false, so it is recommended that it is not equal to! =.
Let a = 0 Portlet b = 0 let c = "0"; A! = b / / falsea! = c / / false number and string's 0 is judged to be equal a! = d / / false 0 and the empty string is judged to be equal a! = = b / / falsea! = = c / / truea! = d / / true after reading this article, I believe you have a certain understanding of "what are the powerful operators in JavaScript", if you want to know more about it Welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.