In JavaScript && arithmetic and | | Operations are used very often, but in some cases do you really understand what they mean?
For example: Today's analysis of the JQ AddClass method appears when this sentence
typeof Value = = = "string" && value
Just saw when really confused, found that the foundation is weak, but also what, immediately Baidu Ah, so found a good article:
First, to say that | | (logical OR), which, literally, returns false only when both the front and back are false, otherwise it returns true.
Alert (true| | false); // truealert (false| | true); // truealert (true| | true); // truealert (false| | false); // false
But, in the deep sense, there is another world, try the following code
Alert (0| | 1);
Obviously, we know that the previous 0 means false, and the next 1 means true, then the result above should be true, and the result of the fact returned is 1. Then look at the following code:
Alert (0| | " A ");
The same front 0 is false "a" is true. The test result is "a", continue to look:
Alert (0| | false)
Both sides are false and the test result is false, for | | Value to the right of the number.
After the above test is not difficult to find, the above code only see | | The value on the left of the number, if the left value is false, take the right value (whatever the right value is); The value to the left of the number is true? Take a look at the following tests:
Alert (2| | 1);
The previous is true followed by true, the test result is 2, then look at one:
Alert (2 | | 0)
The preceding 2 is true, followed by 0 is false and the result is 2.
This test feels so easy, of course I'm trying to illustrate something: or just look at | | The value to the left of the number, and if the value is true, take the left value (regardless of the value on the right). (PS: Right may not be the biological)
"JavaScript logic && with logic or" alert (true&& "a") do You know what pops up?