Summary: boolean operators in JavaScript | and & usage tips, javascript Operators

Source: Internet
Author: User

Summary: boolean operators in JavaScript | and & usage tips, javascript Operators

Have you ever seen such code: a = a | ""; javascript beginners may be confused about this. 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:

<script language="javascript" type="text/javascript">   var a =1;   var b = 0;   var c = 3;   var d = a && b && c;   window.alert(d); </script> 

The output value of d is 0. If the value of d is changed to a value not equal to 0, d will always be 3.

Therefore, in js, "&" returns the first non-true value, that is, 0 (the object can also be). If all are true, the last value is returned.

<script language="javascript" type="text/javascript">   var a =0;   var b = 3;   var c = 5;   var d = a || b || c;   window.alert(d); </script> 

The output value of d is 3. If the value of B is changed to 0, d will always be 5. if all values are changed to 0, the value of d is 0. </p> <p> SO | in js, the first value not false returned is 0 (the object can also be). If all values are false, the last value is returned.
Application:

For example, to verify the mailbox format in a simple way, the format is correct only when both '@' and '.' exist. Otherwise, an error is prompted:

Which one should I use? Let's analyze:

If (form1.elements [3]. value. indexOf ("@", 0) =-1 & form1.elements [3]. value. indexOf (". ", 0) =-1) {alert (" Incorrect EMAIL address! ")}

If both exist: & both sides are false. & Returns the first true value. Therefore, if the condition is false, the statement following the if statement is not executed! The user is not prompted.

Only @ and. A user error is prompted only when the if condition is true.

When both exist, the last value is returned, which is the true if judgment condition is true if statement execution. A user error is prompted.

So use & obvious error!

To replace

If (form1.elements [3]. value. indexOf ("@", 0) =-1 | form1.elements [3]. value. indexOf (". ", 0) =-1) {alert (" Incorrect EMAIL address! ")}

Analysis:

If both exist: if all are false, the return value is false. Therefore, the if condition is not true and the statement is not executed. No error is prompted!

If only one exists, the first value not false is returned. Returns true with an error message!

Both do not exist: the first value not false is returned, and the return value is true. The error message is returned!

So it should be used |

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.