The application of JS short circuit principle the way to streamline code _javascript tips

Source: Internet
Author: User

In the JS logical operation, 0, "", null, False, undefined, Nan all are judged to be false, others are true.

|| Calculates the first operand and, if it can be converted to true, returns the value of the expression on the left, otherwise the second operand is computed. Even if | | The operator's operand is not a Boolean, and it can still be treated as a Boolean or operation, because it can be converted to a Boolean value regardless of the type of value it returns.
The attribute that it returns for a value that is not Boolean: used for a non-boolean operand | |, to select the first defined and Non-null value in a set of alternative values (the first value that is not false)

Cases:
var max = Max_width | | Obj.max_width | | 500;
var attr = attr | | ""; This operation is often used to determine whether a variable is defined, and if it is not defined, give him an initial value, which is useful when defining a default value for a function's arguments.

&&amp, which evaluates to the first expression and, if it is false, does not handle the second expression, otherwise the subsequent expression continues to be processed. Selects from left to right the value of the first expression that is not true, and returns the value of the last expression if it has not been found.

Example: (The taste of which needs to be carefully pondered)

2 && ' s1 ' && ' 123 ' && ' SSS ' the value of the expression equals ' sss '
2 && ' s1 ' && ' && ' SSS ' the value of the expression equals '
2 && ' s1 ' && NaN && ' SSS ' the value of an expression is equal to Nan

if (a >=5) {
Alert ("Hello");
}
can be simplified as:
A >= 5 && alert ("Hello");

typeof 5 and typeof!! 5 of the difference, this is a more rigorous way of writing,!! The role is to convert a variable of another type into a bool type. For example, if (!! attr) => if (attr)

JS Medium | | And && features help us streamline code while also bringing down code readability. It's up to us to weigh it.

JS in the Startwith function of clever implementation, alert (!) Asdf '. IndexOf (' s ') =!0 = True

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.