In the JS logical operation, 0, "", null, False, undefined, Nan all are judged to be false, others are true.
|| Calculates the first operand and, if it can be converted to true, returns the value of the expression on the left, otherwise the second operand is computed. Even if | | The operator's operand is not a Boolean, and it can still be treated as a Boolean or operation, because it can be converted to a Boolean value regardless of the type of value it returns.
The attribute that it returns for a value that is not Boolean: used for a non-boolean operand | |, to select the first defined and Non-null value in a set of alternative values (the first value that is not false)
Cases:
var max = Max_width | | Obj.max_width | | 500;
var attr = attr | | ""; This operation is often used to determine whether a variable is defined, and if it is not defined, give him an initial value, which is useful when defining a default value for a function's arguments.
&&, which evaluates to the first expression and, if it is false, does not handle the second expression, otherwise the subsequent expression continues to be processed. Selects from left to right the value of the first expression that is not true, and returns the value of the last expression if it has not been found.
Example: (The taste of which needs to be carefully pondered)
2 && ' s1 ' && ' 123 ' && ' SSS ' the value of the expression equals ' sss '
2 && ' s1 ' && ' && ' SSS ' the value of the expression equals '
2 && ' s1 ' && NaN && ' SSS ' the value of an expression is equal to Nan
if (a >=5) {
Alert ("Hello");
}
can be simplified as:
A >= 5 && alert ("Hello");
typeof 5 and typeof!! 5 of the difference, this is a more rigorous way of writing,!! The role is to convert a variable of another type into a bool type. For example, if (!! attr) => if (attr)
JS Medium | | And && features help us streamline code while also bringing down code readability. It's up to us to weigh it.
JS in the Startwith function of clever implementation, alert (!) Asdf '. IndexOf (' s ') =!0 = True