&& in Javascript | | Use summary

Source: Internet
Author: User

Prepare two objects for the following discussion

var Alice = {
Name: "Alice",
Tostring:function () {
return this.name;
}
}

var Smith = {
Name: "Smith",
Tostring:function () {
return this.name;
}
}

in JavaScript, && is not just for a Boolean type, but for the result of a Boolean type.
L returns false directly if the first operand is of type Boolean and the value is false.
If the first operand is of type Boolean, and the value is true, and the other operand is of type object, the object is returned.
L If two operands are of type object, then the second object is returned.
L If any one of the operands is NULL, then NULL is returned.
L If any one of the operands is Nan, then the Nan is returned.
L If any one operand is undefinded, then return undefined.


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 just for the Boolean type, but also for returning the result of the Boolean type.
In fact, null, undefined, and NaN will all be treated as false. And the object is treated as true.

L Returns True if the first operand is of type Boolean and the value is true.
If the first operand is of type Boolean and the value is false and the second operand is object, the object is returned.
L returns the first object if all two operands are of type object.
L If two operands are null, then NULL is returned.
L If the two operands are Nan, then the Nan is returned.
L If two operands are undefined, then return to undefined.
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

&& in Javascript | | Use summary

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.