Key point: The logical operator does not return bool and follows the immediate return principle;!! !! The function is to convert a variable of another type into the bool type;
' Returns true; logical operator can replace switch;
The logical operation of the computer, there will be a pursuit of the best implementation, will be as little as possible to deal with the data do not need to access, so the logical operation here can be attributed to
The following logical principles of NA
Learning article called short-circuit principle, I classified as the principle of immediate return , especially for long continuous logic judgment, affecting the return of the results
First, the logical judgment returns only one result, even if it is a continuous logical statement, but the returned result is not bool.
Then, the immediate return to the principle directly affects the return result. Some judgments are not executed, and the results are output.
1. Logic or | |
2. Logic and &&
Application (Elegant):
1. Variable use, given default value
Syntax: var val=value| | '; Here, you can make Val have a default value.
such as: var yahoo = Yahoo | | {}; var yahoo = Yahoo | | {}; This is very widely used.
2. callback function to avoid error
Syntax: Var fn_test=function (callback) {Console.log ("test"); Callback&&callback ();}
Call: Fn_test&&fn_test () fn_test&&fn_test (function () {Console.log ("callback")})
3. Logical operators can replace switch
Syntax: See the original
Attention:
1. In the JS logic operation, 0, ' ', NULL, false, undefined, NaN 0, ', NULL, false, undefined, NaN will be sentenced to false, the others are true. Include ' (Empty
Lattice) is also true
2.!! !! The function is to convert a variable of another type to the bool type.
Resources:
1. Ultra-Practical JavaScript code snippet: http://blog.csdn.net/i10630226/article/details/51938340
The use of JS logical operators