JS Operator | | and &&

Source: Internet
Author: User
Tags case statement switch case

First part (Intro)

JS Operator | | and &&

Let's start by introducing the conversion of the other data types to Boolean in JS to True or false.
JS, it is converted to false when the following values are turned to a Boolean type

0
"" (empty string)
False
Undefined
Null
NaN

The rest will be converted to true.

Here by the way, JS converts data into a Boolean type.
1.Boolean ()
2.!!

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

Ii. Part II (description of the rules)

OK, here's the bottom of it, here are a few rules:
1.
A | | B
When!! A is true when the return value is a; When a is false, the return value is b

2.
A && b
When!! A is true when the return value is B; When a is false, the return value is a

3.
&& priority is higher than | |

4.
There are multiple &&, from left to right 22 execution; | | Similarly

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 &A mp;& 3 | | 0 && 4);//3console.log (0 && 4 | | 1 && 3);//3

Iii. Part III (application)

1.
Callback && callback ();

This must be a stranger, if callback exists, then execute callback (), it is equivalent to
if (callback) {
Callback ();
}


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

This is more commonly used when implementing a simple digital speaker.


3.
var obj = obj | | {};

Generally used for the reference of obj, do a initialization


4.
A reference to an example in http://www.jb51.net/article/21339.htm
Growth rate of 5 shows 1 arrows;
Growth rate of 10 shows 2 arrows;
Growth rate of 12 shows 3 arrows;
Growth rate of 15 shows 4 arrows;
All other displays are displayed with 0 arrows.

Requirements: Displays the number of arrows based on the value of the growth rate

The most common and the easiest way to think about it is the if Else statement, or the switch case statement
This is certainly possible, but in JS there is a more concise approach:

1). var add_step = 12;var Add_level = (Add_step = = 5 && 1) | | (Add_step = && 2) | | (Add_step = = && 3) | | (Add_step = = && 4) | | 0; Console.log (Add_level); 2). var add_step = 120;var Add_level = {' 5 ': 1, ' Ten ': 2, ' n ': 3, ' []: 4}[add_step] | | 0; Console.log (Add_level);

Of course && | | There are many applications, far from the above listed above, in fact, JS in && and | | Very powerful

But one thing to be aware of is: JS | | And && features help us streamline the code while
It also brings down the readability of the code. This and regular expressions are a reason to be able to streamline the code,
However, readability is reduced and it is recommended that you write the necessary notes when using this method.


Iv. Fourth part (comma operator)

Now that we have the operator, here by the way, the comma operator in JS

Defined:
The comma operator, which evaluates the left argument, calculates the right parameter value, and then returns the value of the rightmost parameter.

Let's take a look at 2 cases first:

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: Expression a++, b++, A * b starts from left to right, the value of a becomes 10, The value of B changes to 20,a * b is naturally 200 2.console.log ((1, 0, 3, alert ("KKK")));//kkk

Tip:
However, the comma operator should pay attention when assigning a value:

This will cause an error: var a = 10;var B = a++, 20;console.log (b); This should be related to the priority of "=": var a = 10;var B = (a++, +); Console.log (b);//20

JS Operator | | and &&

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.