Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of JavaScript logic operator and priority

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "introduction to JavaScript logic operators and priorities". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the introduction of JavaScript logic operators and priorities.

I saw a piece of YUI compressor-compressed js code today:

1userNum

& & (ind + = index,ind > = userNum & & (ind-= userNum), ind

< 0 && (ind === -2 && (ind = -1),ind += userNum),selLi.removeClass("on"),$(selLi[ind]).addClass("on")); 直接疯掉了,估计一下子没几个人能看懂。那么就把他"翻译"一下。 && (逻辑与) 这里主要是一个"&&"运算,首先要搞懂这个,看一个简单的例子: 1var a = 1 && 2 && 3;//32var b = 0 && 1 && 2;//03var c = 1 && 0 && 2;//04alert(a),alert(b),alert(c); 嘿嘿,写法很奇怪,运行的结果是3,0,0。一般我们在if语句中经常用到。"&&" (逻辑与) 运算和"||"运算真好相反,"&&" 运算遇到false就返回。 例如: a && b ,如果 a 为true,直接返回b,而不管b为true或者false 。如果 a 为false 那么直接返回a,上面例子中第一个var a = 1 && 2 && 3;因为1 && 2,1为真,返回2;2&&3, 2为真,返回3 。 搞懂了"&&" 运算,再来看最上的面的YUI compressor压缩的js代码,翻译一下: 01if(userNum){02ind += index;03if (ind >

= userNum) {04ind

-= userNum05} 06if (ind

< 0) {07if (ind

= =-2) {08ind

=-1TIX} 10ind

+ = userNum;11} 12selLi.removeClass ("on"); 13 $(Selli [ind]) .addClass ("on"); 14}

Ashamed to say, old, full "translation" for half an hour, or with the help of colleagues to "translate" correctly.

| | (logical or) |

Let's take a look at the "| |" (logical or) operation, and look at the example:

1var a

= 0 | | 1 | | 2Compact 12var b

= 1 | | 0 | | 3scramblink 13alert (a), alert (b)

The "| |" operation returns when it encounters true. For example: a | | b. If an is false, return b directly, regardless of whether b is true or false. If an is true, return a directly instead of continuing.

& & (logic and) and | | (logic or) should pay attention to their priority when they are mixed:

& (logic and) takes precedence over | (logic or)

Return a & & b | | c

To judge the return value according to a, if an is false, it must return c; if b and c are both true, then we can decide whether b or c is based on a, c if an is false, and b if an is true.

Return a | | b & & c

If an is true, return a, whether b or c, if an is false, b if b is false, and c if b is true

1var a

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

= 3 | | 0 & & 2; / /

345var c =

0 | | 2 & & 3; / /

36alert (a), alert (b), alert (c); now that you have a better understanding of "introduction to JavaScript logical operators and priority", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report