JS uses the and or operator priority to implement the if else condition judgment Expression

Source: Internet
Author: User

CopyCode The Code is as follows: <SCRIPT type = "text/JavaScript">
/*************************************** * *************************** Use operator priority to implement ifelse expressions
Result = expression1 & expression2
If both expressions are true, result is true.
If the value of any expression is equal to false, result is false.
JScript uses the following rules to convert non-boolean values to boolean values:
All objects are considered true.
String is considered as false only when it is null.
Null and undefined values are regarded as false.
If and only if it is zero, the value is false.
**************************************** ***************************/
Alert (1 & 0); // false, return the first false's argument 0
Alert (1 & 2); // true, return the last true's argument 2
/*************************************** ****************************
Result = expression1 | expression2
If either of the two expressions is true, the result is true.
JScript uses the following rules to convert non-boolean values to boolean values:
All objects are considered true.
String is considered as false only when it is null.
Null and undefined values are regarded as false.
The value is false only when the value is 0.
**************************************** ***************************/
Alert (1 | 0); // true, return the first ture's argument 1
Alert (0 | false); // false, return the last false 'argument false
/*************************************** ****************************
Returns the last expression.
**************************************** ***************************/
Alert (1, 0,-1); // return the last argument-1
/*************************************** ****************************
Use the operator priority to implement if (...) {...} else {...}
Undefined is returned for all undefined returned values.
Undefined and null will be converted to false
**************************************** ***************************/
VaR A = true;
A & (Alert ('true'), 1) | alert ('false ')
/*************************************** ****************************
Or use the three-object Operator
**************************************** ***************************/
A? Alert ('true'): Alert ('false ');
</SCRIPT>

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.