1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>javascript逻辑与</title>
8 <!--
9 逻辑与(&&)个人理解:
10 前提条件:
11 -逻辑与的判定顺序:从左到右;
12 -只要遇到假/null/undefined/NaN就不会再往下判定。直接返回第一个假/null/undefined/NaN的值
13 -全真时返回最后一个真的值
14 1)操作数都是布尔值
15 全真时:返回true
16 1假或多假:返回false
17 2)操作数为多类型混合(表达式、布尔值等其他类型)
18 1假/多假/全假:返回第一个假
19 全真:返回最后一个真
20 3)特殊情况:操作数为null/undefined/NaN
21 只要含有null/undefined/NaN,就会返回null/undefined/NaN;
22 如果null/undefined/NaN同时存在,则返回null/undefined/NaN中的第一个。
23 -->
24 </head>
25 <body>
26 <script>
27 var a=10,b="10",c=20,d="",e=0,f="dd",g;
28 /* 逻辑与:布尔型 */
29 console.log(a==b && a<c);//true
30 console.log(a==b && a>c);//false
31 /* 逻辑与:混合型 */
32 console.log(e && a);//0,第一个操作数e假,返回第一个,e
33 console.log(a && d);//"",第一个操作数a真,返回第二个,d
34 console.log(a && b && c);//20,a\b\c都为true,返回最后一个,c
35 console.log(a && d && e);//"",a真,d假,e假,返回第一个假,d=""
36 console.log(a && b && c);//20,全真,返回最后一个真
37 console.log(null && a && b && c);//null
38 console.log((a-f) && a && b);//NaN
39 console.log(g && a && b);//undefined
40 console.log(NaN && null && defined);//NaN,这三个谁是第一返回谁
41 </script>
42 </body>
43 </html>