Talking about the sum of JS operator and its priority

  • 2021-07-09 07:11:59
  • OfStack

Today, I saw a piece of YUI compressor compressed js code:

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

Directly crazy, it is estimated that few people can understand it. Then "translate" him once.

& & (Logical AND)

Here is mainly one " & & "Operation, first of all, we must understand this, look at a simple example:

1 var a = 1 & & 2 & & 3;//3

2 var b = 0 & & 1 & & 2;//0

3 var c = 1 & & 0 & & 2;//0

4 alert(a),alert(b),alert(c);

Hehe, the writing is very strange, and the result of running is 3, 0, 0. 1 We often use it in if statements. " & & "(Logical and) operation is the opposite of" "operation is really good." & & "The operation returns when it encounters false.

For example: a & & b, if a is true, return b directly, regardless of whether b is true or false. If a is false, then a is returned directly. The first var a in the above example = 1 & & 2 & & 3; Because 1 & & 2, 1 is true and returns 2; 2 & & If 3 and 2 are true, return 3.

Got it. " & & "Operation, and then look at the uppermost YUI compressor compressed js code, translation 1 below:


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 I am old and have been "translating" for half an hour, but I still have to "translate" correctly with the help of my colleagues.

(Logical or)

Let's look at the "" (logical OR) operation. Look at the example:

1 var a = 0 || 1 || 2;//1

2 var b = 1 || 0 || 3;//1

3 alert(a),alert(b);

The "" operation returns when it encounters true. For example: a b, if a is false, return b directly, regardless of whether b is true or false. If a is true, a is returned directly without proceeding.

& & Pay attention to the priority of (logical and) and (logical or) when they are mixed:

& & (Logical AND) takes precedence over (Logical OR)

return a & & b || c ,

The return value is judged according to a, and c is definitely returned if a is false; If b and c are both true, we can decide whether b or c according to a, return c if a is false, and return b if a is true.

return a || b & & c

According to the priority, it is equivalent to calculating b first & & c, then with a phase or; If a is true, return a, whether b or c, if a is false, return b if b is false, and return c if b is true;

1 var a = 3 & & 0 || 2; //2

3 var b = 3 || 0 & & 2; // 3

5 var c= 0 || 2 & & 3; // 3

6 alert(a),alert(b),alert(c);

Attached: JS operator priority (listed from high to low)

运算符 描述
. [] () 字段访问、数组下标、函数调用以及表达式分组
++ -- - ~ ! delete new typeof void 1元运算符、返回数据类型、对象创建、未定义值
* / % 乘法、除法、取模
+ - + 加法、减法、字符串连接
<< >> >>> 移位
< <= > >= instanceof 小于、小于等于、大于、大于等于、instanceof
== != === !== 等于、不等于、严格相等、非严格相等
& 按位与
^ 按位异或
| 按位或
&& 逻辑与
|| 逻辑或
?: 条件
= oP= 赋值、运算赋值
, 多重求值


Related articles: