& |

Source: Internet
Author: User

 

Under normal circumstances&&And|This is relatively simple and will not be discussed here.

 

Prepare two objects for the following discussion.

 

VaRAlice = {

Name:"Alice",

Tostring:Function(){

Return This. Name;

}

}

 

VaRSmith = {

Name:"Smith",

Tostring:Function(){

Return This. Name;

}

}

 

in JavaScript , for && not only available for Boolean type, it does not only return Boolean type result.

L If the first operand is Boolean type, and the value is false , then return directly false .

LIf the first operand isBooleanType, and the value isTrueAnd the other operand isObjectType, the object is returned.

LIf both operands areObjectType. The second object is returned.

LIf any operand isNull, Then, returnNull.

LIf any operand isNan, Then returnNan.

LIf any operand isUndefinded, Then returnUndefined.

 

 

Alert (False& Alice); // false

Alert (True& Alice); // Alice

 

Alert (Alice & Smith); // Smith

Alert (Smith & Alice); // Alice

 

Alert (Null& Alice); // null

Alert (Nan & Alice); // Nan

Alert (undefined & Alice); // undefined

Alert (Alice & undefined); // undefined

 

For|It is not only usedBooleanType, not only returnsBooleanType.

In fact,Null,Undefined,NanWill be viewedFalse. The object is treatedTrue.

 

L If the first operand is Boolean type, and the value is true , Returns true .

L If the first operand is Boolean type, and the value is false , the second operand is Object , the Object object is returned.

LIf both operands areObjectType, the first object is returned.

LIf both operands areNull, Then, returnNull.

LIf both operands areNan, Then returnNan.

LIf both operands areUndefined, Then, returnUndefined.

 

Alert (False| Alice); // Alice

Alert (True| Alice); // true

 

Alert (Alice | Smith); // Alice

Alert (Smith | Alice); // Smith

 

Alert (Null| Alice); // Alice

Alert (Alice |Null); // Alice

Alert (Null|Null); // Null

 

Alert (Nan | Alice); // Alice

Alert (Alice | Nan); // Alice

Alert (Nan | Nan); // Nan

 

Alert (undefined | Alice); // Alice

Alert (Alice | undefined); // Alice

Alert (undefined | undefined); // undefined

 

 

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.