Short-circuit expressions for JavaScript optimization techniques _javascript Tips

Source: Internet
Author: User

What is a short-circuit expression?

Short-circuit expression: as "&&" and "| |" Operator's operand expression, when evaluated, as long as the final result can be determined to be true or FALSE, the evaluation process terminates, which is called short-circuit evaluation. This is an important attribute of these two operators.

One of the simplest examples:

Copy Code code as follows:

Foo = foo| | Bar

What does this line of code mean? Answer:

Copy Code code as follows:

If foo exists, the value is unchanged, otherwise the value of bar is assigned to Foo
if (!foo)
foo = bar;

In the logical operation of JavaScript, 0, "", null, false, undefined, and Nan are all judged false, while others are true. So in the upper-style foo = foo| | Bar; Medium, | | Calculates the first operand, if it can be converted to true, meaning that Foo already has a value, returns the value of the expression on the left, or the second operand bar.

In addition, even if | | The operand of an operator is not a Boolean value and can still be treated as a Boolean or operation because, regardless of the type of value it returns, it can be converted to a Boolean value.

Of course, the use of the following approach will be more rigorous:

Copy Code code as follows:

if (foo)//Not rigorous

if (!! FOO)//More rigorous,!! Other types of values can be converted to a Boolean type

Can test:

Copy Code code as follows:

var foo;
var number = 1;
var string = "string";
var obj = {};
var arr = [];


Console.log (typeof (foo)); Undefined
Console.log (typeof (number)); Number
Console.log (typeof (String)); String
Console.log (typeof (obj)); Object
Console.log (typeof (arr)); Object

Console.log (typeof (!!)  foo)); Boolean
Console.log (typeof (!!)  number)); Boolean
Console.log (typeof (!!)  string)); Boolean
Console.log (typeof (!!)  obj)); Boolean
Console.log (typeof (!!)  ARR)); Boolean

This can be well met by optimizing JavaScript Engineering as mentioned in the article, making scripts less or not running to optimize JavaScript for the purpose. Note, however, that this writing helps us streamline the code while also bringing down the drawbacks of code readability. So better to do is to add the appropriate annotation.

Related Article

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.