& Amp; and | in Javascript

Source: Internet
Author: User

In general, & | is relatively simple. We will not discuss it here.

Prepare two objects for the following discussion.
Copy codeThe Code is as follows:
Var alice = {
Name: "alice ",
ToString: function (){
Return this. name;
}
}

Var smith = {
Name: "smith ",
ToString: function (){
Return this. name;
}
}

In javascript, for &, it can be used not only for the boolean type, but also for the return of the Boolean type.
L if the first operand is of the Boolean type and its value is false, false is returned directly.
L if the first operand is of the Boolean type and the value is true, and the other operand is of the object type, this object is returned.
L if both operands are of the object type, the second object is returned.
L if any operand is null, null is returned.
L if any operand is NaN, NaN is returned.
L if any operand is undefinded, undefined is returned.


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

| Is also used not only for the Boolean type, but also for the return of the Boolean type.
In fact, null, undefined, and NaN are regarded as false. The object is treated as true.

L if the first operand is of the boolean type and the value is true, true is returned directly.
L if the first operand is of the Boolean type and the value is false, and the second operand is of the object type, the object is returned.
L if both operands are of the object type, the first object is returned.
L if both operands are null, null is returned.
L if both operands are NaN, NaN is returned.
L if both operands are undefined, undefined is returned.
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

We don't need to make such a complicated recommendation. Let's take a look at this part.
A & B: Convert a and B to the Boolean type, and then execute logic and. true returns B, false returns
A | B: Convert a and B to the Boolean type, and then execute the logic or. true returns a, false returns B
Conversion rules:
The object is true.
A non-zero number is true.
The value of a non-null string is true.
Others are false.

For more information, see the following.
Js and or operator |
Js uses the and or operator priority to implement the if else condition judgment Expression
Javascript & | an alternative use technique for arithmetic operations

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.