Using the operator in JScript "| |" and "&&" special features to achieve code streamlining 1th/2 page _javascript tips

Source: Internet
Author: User
Tags logical operators
In the many operators of JScript, three logical operators are provided &&||And !Oh?! is available in advanced languages. According to our normal understanding of the logical operation, the result of the logical operation is ture or false. But the logical operation of JScript is not entirely defined here, only the! operator always returns true| false, while | | and && operations compared to the fun.

JScript for logical operations true| falseis defined in this way:
    • All objects are considered true.
    • The string is considered false if and only if it is empty ("" or ").
    • null and undefined are assumed to be false.
    • The number is false if and only if it is 0 o'clock.

But logical operators | | and && Although follow the above definition rules, but they return the value is very interesting.
For && operations, according to the rules above, the expression if (' abc ' && ' 123 ' && new Date ()) is a true branch of execution, but if this expression is written as:

var value = ' abc ' && '123' && new Date ();

Results Value=fri 00:01:17 utc+0800 2005, which is detected from left to right, and returns that expression if the last expression is true.

for | | Operation, for the following expression:

var value1 = ' abc ' | | '123' | | null | | false;
var value2 = null | |  '' || false | | ' OK ';

Results value1= ' abc ', value2= ' OK '. This is because | | The operation will have a "short-circuit" feature, which is also detected from left to right, except that it is a value that is found to be true and returns the expression immediately.
Such features can help us write thin code, but also bring the code is not easy to read maintenance problems.
I don't know if standard JavaScript supports this because I don't have a browser for NS and Moz at the moment. If it is convenient for you, please tell me the result of the operation.

current 1/2 page   1 2 Next read the full text

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.