The use of JS logical operators

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.