js中的短路运算符主要是&&和||
&&
1.左边的值经过boolean运算为true,返回右边的值
eg:
var a= 1 && "bb";
console.log(a); //bb
2.左边的值为null、undefined或NaN,返回null、undefined或NaN
eg:
var a= null && "34343";
console.log(a); //null
3.左边的值经过Boolean运算为false,返回左边的值
eg:
var a= 1;
var b= a> 10 && a< 5;
console.log(b); //false
--------------------------------------------------------------------------------------------------------------
||
1.左边的值Boolean运算为true,返回左边的值
eg:
var a= 5 || 6;
console.log(a);
2.左边的值Boolean运算为false,返回右边的值
var a= 1;
var b= a>6 || NaN;
console.log(b); //NaN
3.左边为null、undefined、NaN,返回右边的值
eg:
var b= NaN || undefined;
console.log(b); //undefined
时间: 2024-10-06 21:47:06