Let’s talk about logic and (&&), it can be understood from three levels
The first level is the simplest, which is the logical sum between simple boolean values. When both lvalues and rvalues are true, return true, both sides are false or one side of the values are fasle, return false; (AND operation);
The second level, (false, null, indefined, 0,-0, NaN and "" are all false values, and all other values, including objects are true values), perform AND operations on these "true values" and "false values" and return a "true value" or "false value";
It is worth noting that (&&) does not return a boolean value that has always been. It will return the true value or false value that appears at the second level, but what are these "true value" and "false value"? So the third level of understanding is introduced
The third level of understanding is actually simple. When the left operand in the statement is a false value, the right operand of the statement is not calculated, and the left operand is directly returned as the calculation result of the expression; when the left operand is a true value, the value of the right operand is returned as the calculation result of the expression.
For examples as follows:
var o={s:1}; //Create an objectvar p=null; // Created a nullo&& //Return 1, which should be o is the true value, so the returned valuep&& //Return null, which should be p as a false value, so if you do not calculate, you will directly return the value of p <BR>o&&; //This will throw a type error exception because o is the true value and needs to be returned, but it does not exist.
This will be easy to understand
Although && can be used to return true and false values, in most cases, the true value is true and the false value is false;
Let’s talk about logic or (||), learn from one example and apply it to other aspects. Logic and have three levels of understanding of logic or similar, so I won’t write it in detail.
Logic and the most common method is to select a true value expression from a group of alternative operands.
Give an example
var min =min_value||sum.min_value||100;
First find the min_value. If it is not defined, then look in the sum object. If it does not already, you can only assign a dead value of 100 to it.
This method is usually used to set default values for parameters in functions
Give an example
function add(a,b){ b=b||0; return a+b; }
When the initial value is not set to b, b is equal to 0; the add function returns a+0, which is the value of a;
Logical non(!)
His purpose is to inverse the Boolean value of the operand, for example: if x is the true value, !x returns false, if x is the false value, !x returns true.
When returning the Boolean value, these true and false values have been converted into Boolean values and then inverse them.
Here is a kind of introduction! Common usages
Sometimes we need to check whether a variable exists or check whether the value has a valid value, and then use it!!,
For example, to detect whether a value returned from a function is a valid value, use !!student. If it returns true, then it is a valid value.
The above article has a deep understanding of the usage of logical expressions and the usage of or non-existence is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.