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 object var p=null; //Create a nullo&&o.s //Return 1, which should be o is the true value, so return the value of os p&&p.x //Return null, which should be p is the false value, so do not calculate px, and return the value of p directly<br>o&&o.b; //This will throw a type error exception, because o is the true value, and you need to return ob, but ob 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, if a value returned from a function is a valid value, use !!student. If it returns true, then it is a valid value.
The above is the detailed explanation of the usage of versus or non in logical expressions introduced to you by the editor. I hope it will be helpful to you!