zoukankan      html  css  js  c++  java
  • javascript || and &&


     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>javascript || and &&</title>
     6 <script src="../../scripts/jquery.js" type="text/javascript"></script>
     7 <script src="lib/jquery.validate.js" type="text/javascript"></script>
     8 <script type="text/javascript">
     9 $(document).ready(function() {
    10   //jQuery实现log
    11   jQuery.extend({ 
    12     log:function(){console.log(arguments[0])} 
    13   }); 
    14   $.log("A test to understand javascript's "||" and "&&"");
    15 
    16   //先总结归纳一下:
    17   //1、只要“||”前面为false,无论“||”后面是true还是false,结果都返回“||”后面的值。
    18   //2、只要“||”前面为true,无论“||”后面是true还是false,结果都返回“||”前面的值。
    19   //3、只要“&&”前面是false,无论“&&”后面是true还是false,结果都将返“&&”前面的值;
    20   //4、只要“&&”前面是true,无论“&&”后面是true还是false,结果都将返“&&”后面的值;
    21   //5、&&优先级高于||
    22 
    23   // 一、先来说说||,从字面上来说,只有前后都是false的时候才返回false,否则返回true。
    24   $.log(true||false);// true
    25   $.log(false||true);// true
    26   $.log(true||true);// true
    27   $.log(false||false);// false
    28   $.log(0||1); // 1
    29   $.log(2||1); // 2
    30   $.log('a'||1);// a
    31   $.log(''||1); // 1
    32   $.log('a'||0);// a
    33   $.log('a'||'b');// a
    34   $.log(''||0); // 0
    35   $.log(0||''); // ''
    36   $.log(1||'a'&&2);// 1
    37 
    38   function write(msg){ 
    39     for(var i = 0; i < arguments.length; i ++){ 
    40       document.write(arguments[i] + '<br />'); 
    41     } 
    42   } 
    43 
    44   //关于 '&&' 
    45   test1 = 1 && 2 && 3 && 4;//4
    46   test2 = '0' && 2 && 3 && 4; //4
    47   test3 = 1 && 2 && 0 && 4;//0
    48   test4 = 2 && 'i' && 'love' && 3 && 'you';//you
    49   test5 = 'i' && 'hate' && 1 && 0 && 'you';//0
    50   test6 = 1 && false && 'ihateyou' && '2';//false
    51   test7 = 2 && true && 'ihatehateyou' && '23';//23
    52   test8 = 4 && true && 'undefined' && 'true' && '1';//1
    53   test9 = 4 && true && undefined && 'true' && '1';//undefined
    54   test10 = 4 && true && 'null' && 'true' && '1';//1
    55   test11 = 4 && true && null && 'true' && '1';//null
    56   write(test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11); 
    57   write('----------------------------------------------'); 
    58 
    59   //关于 '||' 
    60   _test1 = 1 || 2 || 3 || 4;//1
    61   _test2 = 0 || 2 || 3 || 4;//2
    62   _test3 = 0 || '0' || 8 || 4;//0 
    63   _test4 = 2 || 'i' || 'love' || 0 || 'you';//2
    64   _test5 = 0 || 'hate' || 1 || 0 || 'you';//hate
    65   _test6 = false || 0 || 'ihateyou' || '2';//ihateyou
    66   _test7 = false || true || 'ihatehateyou' || '23';//true
    67   _test8 = 0 || 0 || 'undefined' || 'true' || '1';//undefined
    68   _test9 = 0 || 0|| undefined || 'true' || '1';//true
    69   _test10 = 0 || false || 'null' || 'true' || '1';//null
    70   _test11 = 0 || 0 || null || 'true' || '1';//true
    71   write(_test1, _test2, _test3, _test4, _test5, _test6, _test7, _test8, _test9, _test10, _test11); 
    72   document.close();
    73 
    74 });
    75 
    76 </script>
    77 </head>
    78 <body>
    79 </body>
    80 </html>
  • 相关阅读:
    NYOJ 10 skiing DFS+DP
    51nod 1270 数组的最大代价
    HDU 4635 Strongly connected
    HDU 4612 Warm up
    POJ 3177 Redundant Paths
    HDU 1629 迷宫城堡
    uva 796
    uva 315
    POJ 3180 The Cow Prom
    POJ 1236 Network of Schools
  • 原文地址:https://www.cnblogs.com/xiluhua/p/4376541.html
Copyright © 2011-2022 走看看