Return values of the boolean type conversion operator & amp; and | in javascript, javascriptboolean

Source: Internet
Author: User

Return Value of the boolean type conversion operator & | in javascript, javascriptboolean

Javascript is a weak type language. Any data type can be converted to the boolean type. The conversion rules are as follows:

Value after the data type is converted to boolean

NAN FALSE
Null FALSE
Undefined FALSE
Object TRUE
Function TRUE
0 FALSE
Non-zero digit TRUE

"" (Null String) FALSE
Non-Null String TRUE

 

Knowing this conversion rule is mainly used to learn logical operators & |. In Java, logical operators are only applicable to the boolean type, and the returned results must be of the boolean type. HoweverThe logical operators in javascript can return any type of data, not just true or false..
Many JS frameworks, such as JQuery, have the following code, which is simple, but the premise is that you need to understand what this code returns.

var target = arguments[0] || {}



& | The calculation rules are as follows:

A & B: Convert a and B to the Boolean type, and then execute the logic and. If the result is true, B is returned. If the result is false, a is returned.
A | B: Convert a and B to the Boolean type, and then execute the logic or. If the result is true, return a, false, and return B.

 


Logic or operation of two variables in javascript (this comment is my question)

| And & in javascript are not only used for logical operations. If either of the two operators is null (null or undefined) and the other end is not empty, then | the operation returns a value not empty. & the operation returns the null value.
If you do not understand it, try the following statement:
Alert (null | 'A ')
Alert (null & 'A ')

Type conversion in javascript

In some js engines, we can get 9
But please do not do this!
Converting arrays into numbers is not understandable.
If you do need some nonstandard conversions
ParseInt is recommended.

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.