This article mainly introduces the tips for using boolean operators | and & amp; in JavaScript. It is the basic knowledge in getting started with JavaScript, if you need it, you can refer to whether you have seen such code: a = a | "". javascript beginners may feel confused about it. I will share some of my experiences with you today.
Actually:
a=a||"defaultValue";a=a||"defaultValue";
And:
if(!a){a="defaultValue";}if(!a){ a="defaultValue"; }
And:
if(a==null||a==""||a==undefined){a="defaultValue";}if(a==null||a==""||a==undefined){ a="defaultValue"; }
Is equivalent!
To solve this problem, we must first understand the problem: what happens when the data type in javascript is converted to the bool type.
In javascript, data types can be divided into "true" and "false ". As the name implies, the value of true to bool is true; the value of false to bool is false. The following table lists the values of some common data types when converting to bool:
In the if expression, javascript first converts the conditional expression to the bool type. if the expression is of the true value, the logic in the if statement is executed. Otherwise, it is skipped.
So there is:
if(!a){a="defaultValue";}if(!a){ a="defaultValue"; }
Next let's look at the two expressions "&" and "|.
Because javascript is a weak type language, the two expressions in javascript may be different from those in other languages (such as java.
In javascript, the "&" Operator algorithm is as follows:
If the value of the Left expression is true, the value of the right expression is returned; otherwise, the value of the Left expression is returned.
This means:
Var I = "" & "true"; //-> I = "" I = "true" & "other true values "; //-> I = "other true values" I = "true values" & "; //-> I =" "var I =" "&" true values "; //-> I = "" I = "true" & "other true"; //-> I = "other true" I = "true "&&""; //-> I = ""
The "|" operator has the following algorithm:
If | the value of the Left expression is true, the value of the Left expression is returned; otherwise, the value of the right expression is returned.
This means:
Var I = "" | "true"; //-> I = "true" I = "true" | "other true "; //-> I = "true" I = "true" | ""; //> I = "true" var I = "" | "true "; //-> I = "true" I = "true" | "other true"; //-> I = "true" I = "true" | ""; //-> I = "true"
So we can understand:
a=a||"defaultValue";a=a||"defaultValue";
Logic. If a is a null or empty string ......), "DefaultValue" is assigned to a; otherwise, the value of a is assigned to.
Here we use ||,&&to simplify the program:
Var parameter = ""; function test (parameter) {// return true value return true;} // true value operation function operate1 (parameter) {return "true Value operation ";} // dummy value operation function operate2 (parameter) {return "dummy value operation";} var result = test (parameter) & operate1 (parameter); result = test (parameter) | operate2 (parameter); // equivalent to result = test (parameter )? Operate1 (parameter): operate2 (parameter); alert (result); // true value operation // It is also equivalent to if (test (parameter) {result = operate1 (parameter );} else {result = operate2 (parameter);} alert (result) // true value operation
For example: