Summary of Boolean operators in JavaScript | | _javascript Skills with && skills

Source: Internet
Author: User

Have you ever seen a code like this: a=a| | ""; Perhaps JavaScript beginners will be confused about this. Today I would like to share some of my experience.
In fact:

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 figure this out, first we have to understand a problem: what happens to data types in JavaScript when they are converted to type bool.

In JavaScript, data types can be divided into "truth" and "false values." As the name suggests, the value conversion to bool values is true, and false to bool to false. The following table lists some of the common data types that are converted to bool values:

In an If expression, JavaScript first converts a conditional expression to a bool type, and an expression of true value executes the logic in if, otherwise skips.

So there was:

if (!a) {
a= "defaultvalue";
}


if (!a) { 
  a= "defaultvalue"; 
} 


Let's look at the "&&" and "| |" Two expressions.
Because JavaScript is a weakly typed language, the two expressions in JavaScript may not be the same as 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.

Say:

var i= "&&" Truth Value "//->i=" "
i=" truth "&&" other Truth Value "//->i=" Other Truth value "
i=" truth "&&" "//->i=" "
var i=" "&&" Truth Value "//->i=" " 
i=" truth "&&" other Truth value ",//->i=" other truth value " 
i=" truth value "&&"; /->i= "" 


"| |" The operator's algorithm is as follows:

if | | The value of the left expression evaluates to the value of the left expression, otherwise the value of the right expression is returned.

Say:

var i= "" | | " Truth value "//->i=" truth "
i=" truth value "| |" Other truth values ";//->i=" truth "
i=" truth "| |" "; /->i= "Truth"
var i= "" | | " Truth value "//->i=" truth " 
i=" truth value "| |" Other truth values ";//->i=" truth " 
i=" truth "| |" "; /->i= "Truth value" 



So, you can understand:

a=a| | " DefaultValue ";
a=a| | " DefaultValue "; 

of logic. If a is a false value (equals null, empty string ...). , assign "DefaultValue" to A; otherwise assign the value of a to itself.


Below we use the | |, && to simplify the program:

var parameter= "";
function Test (parameter) {
//return truth return
true;
}
Truth Operation
function operate1 (parameter) {return
"truth operation";
}
The pseudo value Operation
function operate2 (parameter) {return
"false value operation";
}
var result=test (parameter) &&operate1 (parameter);
Result=test (parameter) | | Operate2 (parameter);
Equivalent to
result=test (parameter) operate1 (parameter): operate2 (parameter);
alert (result);//Truth Operation
//also equivalent to
if (test (parameter)) {
result=operate1 (parameter);
} else{
result=operate2 (parameter);
}
Alert (Result)//truth operation

Another 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 value of D is 0, and if the value of D is changed to a value that is not equal to 0, then D will always be 3

So the && in JS returns the first value that is not true is 0 (the object also), if all is true then return the last value.

<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 value of D is 3, and if the value of B is changed to 0, then D will always be 5. If all is changed to 0, then D is 0.</p><p> so JS | | Returns the first value that is not false, which is 0 (the object is also available), and the last value returned if all is false.
Application:

For example, to simply verify the mailbox format, only ' @ ' and '. ' Both exist to calculate the correct format, otherwise the prompt error:

Which one should we use? Let's analyze:

if (Form1.elements[3].value.indexof ("@", 0) ==-1 && form1.elements[3].value.indexof (".", 0) ==-1) {alert (" Email address input Error! ")}

If all exists:&& both sides are false. && returns the first true value, so the If judgment condition is false if the following statement does not execute! The user is not prompted.

Only at least @ and. There is a time when the condition of if is true to prompt the user for an error.

When all two exist, the last value is returned, and is true if the condition is set to the IF statement execution. Prompts the user for an error.

So use && obvious error!

To replace

if (Form1.elements[3].value.indexof ("@", 0) ==-1 | | form1.elements[3].value.indexof (".", 0) ==-1) {alert ("Email address input Error!")}

Analysis:

If two are present: all false, return is false so if condition is not valid, statement does not execute, do not prompt error!

If only one exists: Returns the first value that is not false. Return to True to prompt for error!

Two does not exist: Returns the first value that is not false, and returns a true hint Error!

So you should use | |

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.