Use records of operators & | in js, and js Operators

Source: Internet
Author: User

Use records of operators & | in js, and js Operators

These two operators are often used. They are always mixed and recorded...

A () & B (): If true is returned after a () is executed, B () is executed and the value of B is returned. If false is returned after a () is executed, then the entire expression returns the value of a (), and B () is not executed;
A () | B (): If true is returned after a () is executed, the entire expression returns the value of a (). B () is not executed. If a () is executed () if false is returned, B () is executed and the value of B () is returned;
& The priority is higher than |

The Code is as follows:

Alert (1 & 3 | 0) & 4); // Result 4 ①
Alert (1 & 3 | 0 & 4); // result 3 ②
Alert (0 & 3 | 1 & 4); // Result 4 ③

Analysis

Statement ①: 1 & 3 returns 3 => 3 | 0 returns 3 => 3 & 4 returns 4
Statement ②: Execute 1 & 3 first, return 3, return 0 in execution 0 & 4, and finally compare 3 | 0 returns 3
Statement ③: Execute 0 & 3 first, return 0, return 4 in execution 1 & 4, and finally compare 0 | 4 returns 4

Note: All integers other than 0 are true, and undefined, null, and null strings "" are false.


Is the explanation of logical operators in Js correct?

& In the numeric operation, if the two binary numbers are 1100,101, and the two identical bits are 1, then the bits are 1. Otherwise, the bits are 0, and the result is 1000, in the condition calculation, the result is the same as &, and & will return false when the first result is false, and & will return the value after both expressions are calculated, but the result is the same.

| In bitwise OR numeric operations, if two binary numbers are 1100,101 0, one of the two identical bits is 1, and the other is 1, the result is 1110.
In conditional operations, the result is the same as |. The difference is: | true is returned when the first expression is true, and | two expressions before and after are calculated, even if the first expression is true, the second expression is calculated, but the result is the same.

What is the operator + = in js?

It is a short form. a + = B is a = a + B. There is no difference between the two writing methods. What's more, the former is a little less than the latter's code, looks professional!

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.