就是我们逻辑运算符用于简化判断的一种技巧
规则就是: 取决于谁,就得到谁
console.log(2 || 3); // 2console.log(0 || 3); // 3如果左边是true,得到左边,如果左边是false,得到右边console.log( 2 && 3); // 3console.log(0 && 3); // 0如果左边是true,得到右边,如果左边是false,得到左边
通常我们可以使用它们进行代码的简化
function getSum(a, b, c, d) {// 解决形参个数多于实参的问题 —— 参数的默认值// if(c === undefined){// c = 0;// }// if(d === undefined){// d = 0;// }c = c || 0;d = d || 0;return a + b + c + d;}
function fn(a,f){// 如果函数没有传递进来我们想要调用就会报错// 判断有没有传递进来,如果传递了,我们才调用// if(f !== undefined){// f();// }f && f();}console.log(0||321);//123 因为0表示false,false||??,最终结果是靠??来决定,所有直接输出??console.log(0||321 && 421);//321表示true,true && ?? ,最终结果是靠??来决定,所有直接输出??console.log(1||321);//1 1表示true,true||??因为不管??是什么,前面的1已经表达了结果console.log(1 || 321 && false && 456 && 0 || 12);//1 1表示true,true||??因为不管??是什么,前面的1已经表达了结果console.log(1 && 321 && false && 456 && 0);//false
