逻辑运算
let t1 = null && 1000;
console.log(t1);
let t2 = 1000 && 500;
console.log(t2);
let t3 = false || 500;
console.log(t3);
let t4 = "" || null;
console.log(t4);
let t5 = 1000 || false;
console.log(t5);
// 为变量设置默认值(当未赋值的时候)
// 当bar未定义 (或判为false的值), 赋值100. 否则,继续使用bar的值.
foo = bar || 100;
if (bar)
foo = bar;
else
foo = 100;
console.log(bar);
输出结果:
null
500
500
null
1000
在JS中,逻辑运算法则:
true && X == X
false && X == false
true || X == true
false || X == X
typeof的使用
let t1 = null && 1000;
console.log(typeof t1);
let t2 = 1000 && 500;
console.log(typeof t2);
let t3 = false || 500;
console.log(typeof t3);
let t4 = "" || null;
console.log(typeof t4);
let t5 = 1000 || false;
console.log(typeof t5);
输出结果:
object
number
number
object
number