Summary of boolean operators in JavaScript | and & tips _ javascript skills

Source: Internet
Author: User
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:

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.