1,赋值
在js逻辑运算中,0、""、null、false、undefined、NaN都会判为false,其他都为true
&&:从左往右依次判断,当当前值为true则继续,为false则返回当前值
||:从左往右依次判断,当当前值为false则继续,为true则返回当前值
let temp = null
temp = 1 && true && 'a' // 'a'
temp = 1 && 0 && 'a' // 0
temp = 0 || false || '1' // '1'
temp = 0 || '2' || false // '2'
let x = 15
temp = (x >= 15 && 4) || (x >= 12 && 3) || (x >= 10 && 2) || (x >= 5 && 1) || 0 // 4
temp = { '5': 1, '10': 2, '12': 3, '15': 4 }[x] || 0 // 4
2, 替代if
if(a >=5){alert("你好");}
//可以写成:
a >= 5 && alert("你好");
参考博文:https://segmentfault.com/a/1190000002454280