1 <!DOCTYPE html> 2 <html> 3 ????<head> 4 ????????<meta charset="UTF-8"> 5 ????????<title></title> 6 ?????????7 ????????<script type="text/javascript"> 8 ?????????????9 ????????????/*10 ?????????????* && || 非布尔值的情况11 ?????????????* ????12 ?????????????* ?- 对于非布尔值进行与或运算时,会先将其转换为布尔值,然后再运算,并且返回原值13 ?????????????* ????14 ?????????????* ?- 与运算:15 ?????????????* ????????- 如果第一个值为true,则必然返回第二个值16 ?????????????* ????????- 如果第一个值为false,则直接返回第一个值17 ?????????????* 18 ?????????????* ????- 或运算19 ?????????????* ????????- 如果第一个值为true,则直接返回第一个值20 ?????????????* ????????- 如果第一个值为false,则返回第二个值21 ?????????????* 22 ?????????????*/23 ????????????24 ????????????//true && true25 ????????????//与运算:如果两个值都为true,则返回后边的,谁在后边返回谁26 ????????????var result = 5 && 6;27 ????????????console.log("result = "+result); //6 28 ????????????29 ????????????30 ????????????//与运算:如果两个值中有false,则返回靠前的false31 ????????????//false && true32 ????????????result = 0 && 2;33 ????????????console.log("result = "+result); //034 ????????????35 ????????????result = 2 && 0;36 ????????????console.log("result = "+result); //037 ????????????38 ????????????39 ????????????//false && false40 ????????????result = NaN && 0;41 ????????????result = 0 && NaN;42 ????????????console.log("result = "+result); //043 ????????????44 ????????????//-----------------------------------------------------------------------------45 ????????????46 ????????????//true || true47 ????????????//如果第一个值为true,则直接返回第一个值48 ????????????result = 2 || 1;49 ????????????console.log("result = "+result); //250 ????????????51 ????????????result = 2 || NaN;52 ????????????console.log("result = "+result); //253 ????????????54 ????????????result = 2 || 0;55 ????????????console.log("result = "+result); //256 ????????????57 ????????????58 ????????????//如果第一个值为false,则直接返回第二个值59 ????????????result = NaN || 1;60 ????????????console.log("result = "+result); //161 ????????????62 ????????????result = NaN || 0;63 ????????????console.log("result = "+result); //064 ????????????65 ????????????result = "" || "hello";66 ????????????console.log("result = "+result); //hello67 ????????????68 ????????????result = -1 || "你好";69 ????????????console.log("result = "+result); //-170 ????????????71 ????????????72 ????????</script>73 ????????74 ????</head>75 ????<body>76 ????</body>77 </html>
JS基础_非布尔值的与或运算
原文地址:http://www.cnblogs.com/ZHOUVIP/p/7652796.html