First, logic and (&&), it can be understood from three levels
The simplest of the first level is the logic between simple Boolean values, where both the left and right values are true, return true when both sides are false or the values on either side are Fasle and return false;
The second level, (False,null,indefined,0,-0,nan and "" These are false values, all other values including the object are true), to these "truth" and "false value" and operation, return a "truth" or "false value";
It is noteworthy that (&&) does not return has been a Boolean value, he will return the second level appears in the truth or false value, but these "truth" and "false value" in the end is what value? So introducing the third level of understanding
The third level of understanding, in fact, is also simple, when the left operand in a statement is a false value, the right-hand operand of the statement is not evaluated, and the left-hand operand is returned directly as the result of the operation of the expression; when the left-hand operand is true, the value of the right-hand operand is returned as the result of the expression's operation,
Here are some examples:
var o={s:1}; Create an object
var p=null;//Create a null
O&&O.S//return 1, should be O is true, so return O.S value
p&&p.x//return null, should be p is false value, So do not compute p.x, directly return the value of P <br>o&&o.b; This throws a type error exception because O is true and needs to return to O.B, but O.B does not exist.
That's a good idea.
Although && can be used to return truth and false values, in most cases the truth value is true and false values are false;
Again, logic or (| |), extrapolate, logic, and also with logical or similar three levels of understanding, are not written in detail
The logical and most commonly used method is to select a true value expression from a group of alternative operands
As an example
Find Min_value First, if it is not defined, find it in the Sum object, and if not, assign it a dead value of 100,
This method is usually used to set the default value for a parameter in a function
As an example
function Add (a,b) {
b=b| | 0;
return a+b;
}
When the initial value is not set to B, B equals the 0;add function returns the A+0, which is the value of A;
Logical NON (!)
His aim is to reverse the Boolean value of the operand, for example: If X is true,!X returns false, and!x returns real if X is a false value.
When you return a Boolean value, you have converted these truth values, false value, to a Boolean value, and then you reverse it.
Here to introduce a! Common usage of
Sometimes we need to check whether a variable exists or if the value has a valid value, use the!!,
For example, to detect whether a value returned from a function student is a valid value, use!! Student, if he returns true, then he is a valid value.
The above is a small set to introduce the logic of the expression and or not in the usage of the relevant knowledge, I hope to help you!