Summary of js operators "|" and "&"

Source: Internet
Author: User

In almost all languages, | and & follow the "Short Circuit" principle. For example, if the first expression in & is false, it will not process the second expression, but | is the opposite. Js also follows the above principles. But it is interesting that they return values.

Logical operators & |.

Code:

The Code is as follows: Copy code
Var attr = true & 4 & "aaa ";


The running result of attr is not simply true or false, but "aaa"
Let's take a look. |:
Code: var attr = attr | ""; this operation is often used to determine whether a variable has been defined. If it is not defined, an initial value is given, this is useful when defining a default value for function parameters. Because js does not support defining func ($ attr = 5) directly on type parameters like php ). Remind you again to remember the above principle: if the real parameter needs to be 0, "", null, false, undefined, NaN, it will also be treated as false.

Common method:

The Code is as follows: Copy code

Var a, B;
If (! A ){
A = B;
}

Or

If (a> = 5 ){

Alert ("hello ");
}

Can be written:

A> = 5 & alert ("hello ");


This requires only one line of code. Note that the features of | and & in js help us streamline the code and reduce the readability of the Code. This requires us to weigh ourselves.

1. Logic or operator |:

When the operator | is a Boolean value, it performs a Boolean OR operation on the two operators.

It calculates the first operation number first. If the value of this expression can be converted to true, it returns the value of the expression on the left. Otherwise, calculate the second operation.

Even if | the operator's number of operations is not a Boolean value, it can be considered a Boolean OR operation, because whatever type of value it returns can be converted to a Boolean value.

On the other hand, we use | for non-Boolean operations, which uses the features that will be returned by non-boolean values. This operation usually selects the first defined and non-null value in a set of optional values (that is, the first value will not be converted to false)

Example:

The Code is as follows: Copy code

Var max = max_width | preferences. max_width || 500

Or method:

Var a, B;
A = a | B;


The following describes the rules for displaying the growth rate:

1 arrow is displayed when the growth rate is 5;

The growth rate is 10 and two arrows are displayed;

3 arrows are displayed at a speed of 12;

4 arrows are displayed at a rate of 15;

Each other shows 0 arrows.

How to implement it using code?

Let's take a look at the powerful expressiveness of js:

The Code is as follows: Copy code

Js Code
 
Var add_level = (add_step = 5 & 1) | (add_step = 10 & 2) | (add_step = 12 & 3) | (add_step = 15 & 4) | 0;

More powerful and better:

The Code is as follows: Copy code

Var add_level = {'5': 1, '10': 2, '12': 3, '15': 4} [add_step] | 0;


Second requirement:

The Code is as follows: Copy code

Var add_level = (add_step> 12 & 4) | (add_step> 10 & 3) | (add_step> 5 & 2) | (add_step> 0 & 1) | 0;

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.