A deep understanding of the logical operators (and, or) and JavaScript operators of javascript
Over half of December, winter is a wonderful season. The cold air forces people to live in a comfortable and comfortable environment. In winter, it will give people a quiet and peaceful atmosphere, which will bring people into it, as if it is the end of an old stage and the beginning of a new stage. In this case, it is not unreasonable that Christmas and Spring Festival in the west and China are both in the winter. In the coldest time of the year, people are clustered in a warm environment, they told each other about their achievements in the past year, looked forward to the beautiful wishes of the New Year, and joined each other. The cold weather and the warmth of human feelings formed a strong comparison. In the cold weather, it seems that it is more conducive to people's thinking, to explore the true meaning of knowledge.
This time I want to share the logic operators and, or, that is&, |The new students will feel boring when they see this. What is the purpose of sharing this? Will it happen when I first start learning JS, I have used it for countless times. Experienced students may fall into meditation, but what are the mysteries of this? That's right. Don't look at these simple operators. Although this is the most basic knowledge, the hidden mysteries are intriguing, next, I will unveil the wonders behind this short answer operator.
I will not talk about the basic functions. Both symbols can be understood by a programmer. Here I want to talk about Implicit conversions in JS.
As we all know, JS automatically performs implicit conversion of non-boolean values during logical judgment, converts them to boolean values, and performs logical operations. When you are a beginner in JS, it will be said that in implicit conversion, except for a few specific false values, other values will be converted to true values. These false values include:
NaN; ""; undefined; null; 0;
With these implicit conversion rules, the core foundation of logical operations in JS is formed.
In fact, it is not entirely correct to say "logical operators" in Javascript. Kyle Simpson mentioned in the book "You Don't Know JS" series: "It's not so much a 'logical operator ', it is better to say'Selector operator'." Why do masters say this? In fact, most of us are blinded by the appearance of JavaScript, such as the following simple code:
if( "hello" && 0 ) { console.log(true); } else { console.log(false); }
If you do not have a deep understanding of JS, you may explain this Code as follows: first, in logical judgment, "hello" is a true value, and 0 is a false value, A true value and a dummy value are operated on and the result is false. This may also be the understanding of most people, but in fact it is not the case that the internal principle is not that simple, because & and | the returned results are not true or false judgment conditions, instead, it judges an original value in the condition. It will judge the values in the condition judgment in sequence. If it is a non-Boolean value, it will be converted to a Boolean value for judgment, and then decide which value to return based on the judgment condition.
For the &: operator, the first dummy value in the Condition Statement is returned. If all values are true, the last value is returned. & is also called the "daemon operator ". For example, the following code:
var a = "hello" && "world"; console.log(a); //world var b = 0 && 1; console.log(b); //0
It can be seen that the logical operator returns not the true or false condition, but the original value. If the condition statement contains multiple & operators, the above principles are followed and the values are determined from left to right. If a false value is encountered, the false value is returned. If all values are true, returns the last value.
For |: This operator is opposite to the & operator. It returns the first true value in the condition statement. If all values are false, the last value is returned. For example, the following code:
var a = "hello" || 0; console.log(a); //hello var b = 0 || NaN; console.log(b); //NaN
Similarly, | the returned value is not a Boolean value. If there are multiple |, scan from left to right according to the same principle.
Here we come to the core of this article,In JS, the condition judgment statement is based on implicit conversion. That is to say, the so-called logical operator is actually scanning from left to right in the condition judgment statement. If it is a Boolean value, it determines whether the Boolean value is true or false. If it is a non-Boolean value, it is implicitly converted before being true or false. If the condition is met, the value is returned, if the condition value is not met, the last value is returned, and then the returned value is judged. If it is a Boolean value, it is directly judged. If it is a non-Boolean value, it is converted to a Boolean value by implicit conversion before judgment.So we can also call &False Operators", Called |True Operators", Because the essence of these two operators is to take the first true or false value in the condition statement. If it is not found, the last value is returned. Such an algorithm meets the needs of logical judgment. For example, if all values are true values, it doesn't matter which value is returned, because all values can be implicitly converted to true, and as long as there is a false value, the judgment condition is not true, so the first encountered false value is returned. | Operator. If all values are false values, any returned value is implicitly converted to false. However, if a true value is returned, the condition is true, therefore, the first encountered true value is returned. & | The operators are all short-circuited.
Therefore, we can implement a logic operation function by 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 is explained, but for content and space issues, we will not go into depth for the time being, but will only give a brief description. ! In fact, the operating mechanism of the operator is the same as & |. First, the parameter value is judged. If it is a Boolean value, the inverse operation is performed, if it is a non-Boolean value, the implicit conversion is performed first, and then the inverse operation is performed. The if (something) statement we usually write actually means if (!! Something ))
Then we can use:
var a = ["hello", undefined, "world"]; console.log(AND.apply(null, a)); //undefined var b = ["", 0, NaN]; console.log(OR.apply(null, b)); //NaN
Then, we can draw a conclusion:
A = x | y; // equivalent to: a = x? X: y; a = x & y; // equivalent to: a = x? Y: x;
This is usually what some compression tools do. They try to convert complicated condition judgment statements into & or |, because the code is more streamlined, but the readability is not that impressive.
For the first piece of code:
if( "hello" && 0 ) { console.log(true); } else { console.log(false); }
We need to explain this as follows: First, this is an operator. The function of the operator is to take the first dummy value. If all values are true, the last value is returned. Therefore, in this statement, the first value is "hello", because this value is a non-Boolean value, the JS engine will first implicitly convert it to a Boolean value, this value is not in the scope of the dummy value, so it is converted to true. Then, the JS engine will continue searching. The second value is 0, which is also not a Boolean value. Therefore, the JS engine will first implicitly convert it to a Boolean value, this value is within the range of the dummy value, so it is converted to false. If the Search Condition of the & operator is met, the value 0 is returned. The conditional judgment statement accepts the value 0, which is not a Boolean value. Therefore, it is implicitly converted, and the value is within the scope of the dummy value, therefore, it will be converted to false, and then the console will output false.
So when we see & | in the future, we should not simply understand it in the literal sense. After reading this article, you can be very proud to say to others, do you really use logical operators?
Well, there are so many essence behind these two gadgets. It seems that the depth of knowledge is infinite, and winter is a season that can inspire people to think. Christmas is coming soon. We wish you a Merry Christmas and Merry Christmas!
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!