Use the special features of operators "|" and "In JScript to streamline code page 1/2

Source: Internet
Author: User

Among the many operators in JScript, three logical operators are provided. &&, |And !Oh ?! Is provided in advanced languages. According to our normal understanding of logical operations, the logical operation result should be true or false. However, the logic operations of JScript are not exactly defined in this way. Only! The true | false operator is always returned, and | is more fun.

JScript defines true or false for logical operations as follows:

  • All objects are considered true.
  • String is considered as false only when it is null ("" or.
  • NullAnd undefined are regarded as false.
  • The value is false only when the value is 0.

But logical operators|And&&Although the above defined rules are followed, the returned values are very interesting.
For & operations, according to the above rules, if ('abc' & '000000' & new Date () is the true branch, but this expression is written as follows:

Var value = 'abc' & '000000' & new Date ();

Result value = Fri Jan 21 00:01:17 UTC + 0800 2005. The original value is checked from left to right. If the last expression is true, the expression is returned.

For | operations, the following expressions are used:

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

Result value1 = 'abc', value2 = 'OK '. This is because | the operation has a "Short Circuit" feature, which is also checked from left to right, but it is an expression that immediately returns a true value.
This feature can help the Group to write concise code, but it also brings about the problem that the Code is not easy to read and maintain.
Since I have no NS or moz browsers at hand, I don't know whether standard JavaScript supports this as well? For your convenience, please report the result after running.

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: 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.