JavaScript logical operators and priority

Source: Internet
Author: User

Today I read a piece of js Code compressed by YUI compressor:

userNum && (ind += index,ind >= userNum && (ind -= userNum),ind < 0 && (ind === -2 && (ind = -1),ind += userNum),selLi.removeClass("on"),$(selLi[ind]).addClass("on"));

I went crazy, and it was estimated that few people could understand it all at once. Let's translate it.

& (Logical and)

 

Here is a "&" operation. First of all, you must understand this. Let's look at a simple example:

var a = 1 && 2 && 3;//3var b = 0 && 1 && 2;//0var c = 1 && 0  && 2;//0alert(a),alert(b),alert(c);

Hey, it's strange to say that the running result is 3, 0, and 0. It is usually used in if statements. "&" (Logical and) operations and "|" operations are opposite. If "&" operations are false, return.
For example: a & B. If a is true, B is directly returned, regardless of whether B is true or false. If a is false, a is directly returned. In the preceding example, the first var a = 1 & 2 & 3; returns 2; 2 & 3 because 1 & 2, 1 is true, 2 is true, and 3 is returned.
After understanding the "&" operation, let's take a look at the top YUI compressor compressed js Code:

if(userNum){ind += index;if (ind >= userNum) {ind -= userNum}if(ind < 0){if(ind === -2){ind = -1;}ind += userNum;}selLi.removeClass("on");$(selLi[ind]).addClass("on");}

I am ashamed to say that it took me half an hour to "translate" when I was too old. It was still correct to "translate" with the help of my colleagues.

| (Logical or)

Let's take a look at "|" (logical or) operations. Let's look at the example:

var a = 0 || 1 || 2;//1var b = 1 || 0 || 3;//1alert(a),alert(b);

"|" Is returned if the operation is true. For example, a | B. If a is false, B is directly returned, regardless of whether B is true or false. If a is true, a is returned directly without further execution.

& (Logical and) and | (logical or) when mixed, pay attention to their priority:

& (Logical and) the priority is higher than | (logical or)
Return a & B | c,
Judge the return value based on a. If a is false, c is certainly returned. If B and c are both true, then we can determine whether B or c is based on, if a is false, c is returned. If a is true, B is returned.
Return a | B & c
Calculate B & c according to the priority, and then phase a or; if a is true, return a, whether B or c. If a is false, if B is false, B is returned. If B is true, c is returned;

var a = 3  &&  0 || 2;  //2var b = 3 || 0  &&  2; // 3var c= 0 || 2 && 3; // 3alert(a),alert(b),alert(c);

Source: http://www.css88.com/archives/3770

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.