In the middle of December, Winter was a wonderful season, and the cold air forced people to live in an easy and comfortable environment. Winter will give a quiet and peaceful atmosphere, let people immersed in it, as if an old stage of the end, but also a new stage of the beginning. So, the west and China's Christmas and Spring Festival are chosen in the winter is not unreasonable, in the coldest of the year, people are clustered in warm surroundings, telling each other about their achievements over the past year, looking forward to the New Year's good wishes, the reunion of those who miss each other, The cold of the weather and the warmth of human feelings form a strong contrast. And in the cold, as if more conducive to people to think, to explore the true meaning of knowledge.
This time to share is the logical operators in JS and, or, that is, &&, | | , newcomers to see here will feel boring, this thing has what good to share, just start learning JS when not will it, I used countless times there is no problem ah. and experienced students may be immersed in meditation, it is difficult to find out what the mystery lies? Yes, do not look at this simple few operators, although this is the most basic knowledge, but the hidden mysteries are very intriguing, and then I will be the one to uncover the simple answer to the magic behind the operator.
The basis of the role I do not say, these two symbols is a programmer can understand, here first I would like to first say JS in the implicit conversion.
It is well known that JS automatically converts the value of a non boolean type into a Boolean value and then makes a logical operation when making logical judgments. In the beginner JS, it will be mentioned in the implicit conversion, in addition to a few specific false values, the other will be converted to true value, these false values are:
NaN;
"";
undefined;
null;
0;
With these implicit conversion rules, it constitutes the core of the logic operation in JS.
In fact, in JS, to say "logical operator" is not entirely correct, Kyle Simpson in the "You Don ' t Know JS" series of Books, said: "Not so much a ' logical operator ', rather a ' selector operator '." "Why did the master say so?" In fact, most of us are blinded by the appearance of JS, such as the following a very simple code:
if ("Hello" && 0) {
console.log (true);
} else {
console.log (false);
}
If you do not understand JS deeply enough, you may interpret this code: first in the logical judgment, "Hello" is a truth, 0 is a false value, a truth and a false value of the operation, the result is false. This may also be the understanding of most people, but it is not, the internal principle is more than that simple, because && and | | Return is not to determine the true and false conditions , but to determine a raw value in the condition. in turn, it will judge the value in the conditional judgment, if it is a Boolean value, then convert it to a Boolean to make a judgment, and then decide which value to return according to the judgment condition.
For &&: This operator returns the first false value in a conditional statement, and if all values are true , the last value ,&& is also called a "daemon operator." For example, the following code:
var a = "Hello" && "world";
Console.log (a); World
var b = 0 && 1;
Console.log (b); 0
As you can see, the logical operator actually returns not the true and false of the condition, but the original value. If you have more than one && operator in a conditional statement, follow the above principles and judge from left to right, and if you encounter a false value, return the false value, and return the last value if all the values are true.
for | | : This operator, in contrast to the && operator, returns the first true value in a conditional statement and returns the last one if all the values are false . For example, the following code:
var a = "Hello" | | 0;
Console.log (a); Hello
var b = 0 | | NaN;
Console.log (b); NaN
Similarly, | | Nor is the Boolean value returned. If there are multiple | | The same principle is followed, scanned from left to right.
And here comes the core of this article, in JS, conditional judgment statements are based on implicit conversion, that is to say, the so-called logical operators, in fact, in the conditional judgment statement from left to right in turn, if it is a Boolean value, then judge the Boolean value of True and false, if it is a non-boolean value, The value is then implicitly converted and then judged to be true or false, and if the condition is met, returns the value, returns the last value if the condition value is not met, and then makes a judgment on the returned value and, if it is a Boolean, directly determines that, if it is a non boolean value, it is implicitly converted to a Boolean value before being judged. so we can also call the && " take false operator ", put | | Called the take-true operator , because the essence of both operators is the first truth or false value in a conditional statement, and the last value is returned if it is never found. And such an algorithm just satisfies the need of logical judgment, for example, the && operator, if all values are true, then the return of which value does not matter, because all values can be implicitly converted to true, and as long as there is a false value, the judgment condition is not valid, so will return the first encountered false value. and | | operator, if all of the values are false, any return will be implicitly converted to false, but as long as a true value is encountered, the judgment condition is set, so the first truth encountered is returned. && and | | Operators are "short-circuit".
So we can implement a function of a logical operation ourselves:
&& equivalent to:
function and () {for
(var i = 0; i < arguments.length i++) {
if (!arguments[i]) {
return arguments[i];
}
return arguments[i-1];
}
// || Equivalent to:
function OR () {for
(var i = 0; i < arguments.length; i++) {
if (arguments[i)) {return
arguments[ I];
}
return arguments[i-1];
}
(Note: I also want to be right here!) This operator to explain, but take into account the content and length of the problem, temporarily do not do in-depth research, just do a simple story. Operators actually run mechanisms with && and | | Is the same, first of all, we will judge the value of the parameter, if it is a Boolean value, then take the inverse operation, if it is a non-boolean value, then the first implicit conversion, and then take back operations. And we usually write the IF (something) statement, actually meaning if (!! Something))
Then we can use this:
var a = ["Hello", Undefined, "world"];
Console.log (and.apply (null, a)); Undefined
var b = ["", 0, NaN];
Console.log (or.apply (null, b)); NaN
And then we can deduce the conclusion:
A = x | | y;
Equivalent to:
a = x x:y;
A = x && y;
Equivalent to:
a = x y:x;
This is often what some compression tools do, as much as possible to convert complex conditional judgment statements into && or | | , because the code is more streamlined, but the readability is less impressive.
For the first piece of code:
if ("Hello" && 0) {
console.log (true);
} else {
console.log (false);
}
We're going to explain this now: first of all, this is an operator, and the operator's role is to take the first false value, if all the values are true, then return the last value. So in this statement, the first value is "Hello", because the value is a non boolean value, and the JS engine converts it implicitly to a Boolean value that is not in the range of the false value, so it is converted to true. Then the JS engine will continue to look up, the second value is 0, the value is also not a Boolean value, so the JS engine will also implicitly convert it to a Boolean value, which is in the range of the false value, so it is converted to false to satisfy the && operator's lookup condition, and the value 0 is returned. The conditional judgment statement accepts a value of 0, which is not a Boolean, so it is implicitly converted, and the value is in the range of false values, so it is converted to false and the console outputs false.
So later when we see the && and | | Time, do not just the literal meaning to understand, after reading this article, you can be very proud to say to others, you really will use the logical operator?
Okay, so there are so many two gadgets behind the essence, it seems that the depth of knowledge is infinite, winter is really a can cause people to think of the season. Christmas is coming and I wish you a Merry Christmas in advance, Merry christmas!
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!