&& and ||并不一定返回bool类型。通过布尔短路可以实现分支语句,而使得代码更加简洁。
switch(something){
case 1:
return dosomething1();
break;
case 2:
return dosomething2();
break;
case 3:
return dosomething3();
break;
}
等于:
var res = something==1?dosomething1():something==2?dosomething2():something==3&&dosomething3();