Operators in js | and & amp;

Source: Internet
Author: User

Operators in js | and &&

1. Part 1 (Introduction)

Operators in js | and &&

First, we will introduce whether to convert other data types to boolean in js to true or false.
In js, when the following values are converted to the boolean type, the values are converted to false.

0
"" (Empty string)
False
Undefined
Null
NaN

The rest will be converted to true

Here, by the way, js converts data to the boolean Type by using the following methods:
1. Boolean ()
2 .!!

 

//egvar value = 5;console.log(typeof value);//numberconsole.log(typeof Boolean(value));//booleanconsole.log(typeof !!value);//boolean

 

Ii. Part 2 (rule description)

Now let's get down to the point. Here are some rules:
1.
A | B
When !! If a is true, the returned value is a. When !! If a is false, the return value is B.

2.
A & B
When !! When a is true, the returned value is B. When !! When a is false, the returned value is.

3.
& The priority is higher than |

4.
There are multiple &, which are executed in pairs from left to right. | likewise

 

//egconsole.log(true || 0);//trueconsole.log(0 || 1);//1console.log(null || undefined);//undefinedconsole.log(undefined || null);//nullconsole.log(null && "rgy");//nullconsole.log(undefined && "rgy");//undefinedconsole.log(1 || 2 || 4);//1console.log(1 && 2 && 4);//4console.log(1 && 0 && 4);//0console.log((1 && 3 || 0)) && 4);//4console.log(1 && (3 || 0 && 4));//3console.log(1 && 3 || 0 && 4);//3console.log(0 && 4 || 1 && 3);//3

 

3. Part 3 (Application)

1.
Callback & callback ();

This must be familiar to everyone. If callback exists, call back () is executed, which is equivalent
If (callback ){
Callback ();
}


2.
This. count = this. count | 0;

This is often used to implement a simple calculator.


3.
Var obj = obj | {};

It is generally used to upload the obj of the parameter and initialize the object.

 

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

Requirement: displays the number of arrows based on the growth rate value.

The most common and easiest way to think about it is simply an if else statement or a switch case statement.
This is certainly possible, but there are more concise methods in js:

 

1).var add_step = 12;var add_level = (add_step == 5 && 1) || (add_step == 10 && 2) || (add_step == 12 && 3) || (add_step == 15 && 4) || 0; console.log(add_level);2).var add_step = 120;var add_level = {'5':1,'10':2,'12':3,'15':4}[add_step] || 0; console.log(add_level);

 

Of course, & | there are many other applications, which are far from limited to those listed above. In fact, js & | is very powerful.

However, it should be noted that the features of | and & in js help us streamline the Code while at the same time,
It also reduces the readability of the Code. This is in line with regular expressions and can streamline code,
However, the readability is reduced. We recommend that you write the necessary comments when using this method.

 

Part 4 (comma operator)

Now that we have mentioned the operator, let's take a look at the comma operator in js.

Definition:
The comma operator calculates the parameter on the left, the parameter value on the right, and returns the value of the rightmost parameter.

Let's take a look at two cases:

 

1. var a = 9, B = 19; function test () {return a ++, B ++, a * B;} var c = test (); console. log (a); // 10console. log (B); // 20console. log (c); // 200 analysis: The expression a ++, B ++, a * B is calculated from left to right. The value of a is changed to 10, and the value of B is changed to 20, the value of a * B is naturally 200. console. log (1, 0, 3, alert ("kkk"); // kkk

Tip:
Note the following when assigning values to a comma OPERATOR:

 

 

In this case, the following error occurs: var a = 10; var B = a ++, 20; console. log (B); this should be related to the priority of "=" and changed to this line: var a = 10; var B = (a ++, 20); console. log (B); // 20

 

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.