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.