js 不同类型var的boolean运算验证

jsCode

var a = '0';
var b = '1';
var c = false;
var d = true;
var e = 0;
var f = 1;

console.log("var a = '0';\nvar b = '1';\nvar c = false;\nvar d = true;\nvar e = 0;\nvar f = 1;");

console.log('\n');

console.log("a && c" + " ---> " + (a && c));
console.log("a && d" + " ---> " + (a && d));
console.log("b && c" + " ---> " + (b && c));
console.log("b && d" + " ---> " + (b && d));

console.log("c && a" + " ---> " + (c && a));
console.log("d && a" + " ---> " + (d && a));
console.log("c && b" + " ---> " + (c && b));
console.log("d && b" + " ---> " + (d && b));

console.log('\n');

console.log("a || c" + " ---> " + (a || c));
console.log("a || d" + " ---> " + (a || d));
console.log("b || c" + " ---> " + (b || c));
console.log("b || d" + " ---> " + (b || d));

console.log("c || a" + " ---> " + (c || a));
console.log("d || a" + " ---> " + (d || a));
console.log("c || b" + " ---> " + (c || b));
console.log("d || b" + " ---> " + (d || b));

console.log('\n');

console.log("e && c" + " ---> " + (e && c));
console.log("e && d" + " ---> " + (e && d));
console.log("f && c" + " ---> " + (f && c));
console.log("f && d" + " ---> " + (f && d));

console.log("c && e" + " ---> " + (c && e));
console.log("d && e" + " ---> " + (d && e));
console.log("c && f" + " ---> " + (c && f));
console.log("d && f" + " ---> " + (d && f));

console.log('\n');

console.log("e || c" + " ---> " + (e || c));
console.log("e || d" + " ---> " + (e || d));
console.log("f || c" + " ---> " + (f || c));
console.log("f || d" + " ---> " + (f || d));

console.log("c || e" + " ---> " + (c || e));
console.log("d || e" + " ---> " + (d || e));
console.log("c || f" + " ---> " + (c || f));
console.log("d || f" + " ---> " + (d || f));

console.log('\n');

console_log

0.js:8 var a = '0';
var b = '1';
var c = false;
var d = true;
var e = 0;
var f = 1;
0.js:10 

0.js:12 a && c ---> false
0.js:13 a && d ---> true
0.js:14 b && c ---> false
0.js:15 b && d ---> true
0.js:17 c && a ---> false
0.js:18 d && a ---> 0
0.js:19 c && b ---> false
0.js:20 d && b ---> 1
0.js:22 

0.js:24 a || c ---> 0
0.js:25 a || d ---> 0
0.js:26 b || c ---> 1
0.js:27 b || d ---> 1
0.js:29 c || a ---> 0
0.js:30 d || a ---> true
0.js:31 c || b ---> 1
0.js:32 d || b ---> true
0.js:34 

0.js:36 e && c ---> 0
0.js:37 e && d ---> 0
0.js:38 f && c ---> false
0.js:39 f && d ---> true
0.js:41 c && e ---> false
0.js:42 d && e ---> 0
0.js:43 c && f ---> false
0.js:44 d && f ---> 1
0.js:46 

0.js:48 e || c ---> false
0.js:49 e || d ---> true
0.js:50 f || c ---> 1
0.js:51 f || d ---> 1
0.js:53 c || e ---> 0
0.js:54 d || e ---> true
0.js:55 c || f ---> 1
0.js:56 d || f ---> true
0.js:58 

summary

  • String不为null或”“即视为真;
  • 运算从左侧开始判断,若运算符左侧能直接判断结果(&&需左为false,||需左为true,而非Empty的String均为真)则直接输出相应结果(String或Boolean);
  • 结果需要取舍时取右边(即运行的最后一步);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值