Why would you use this syntax?
var myVar = myArray.length && myArray || myObject;
instead of
var myVar = myArray.length ? myArray : myObject;
Edit: I just had a thought that if in the case of the && || syntax both sides of the || evaluated to false, as you might expect if myObject was undefined or null, if false would be returned. But it isn’t, the objects value undefined or null is returned.
true || true //true
true || false //true
false || true //true
false || false //false
Edit2:
!!(myArray.length ? myArray : myObject);
This does however return false if myObject is undefined or null
x && y || zis different thanx ? y : zeven if it “works” in the case presented.Consider when
yevaluates to a false-value (in the postycan’t be a false-value when it is evaluated because it is dependent uponxand thus it “works” as expected).Using
?:is the much better and more clear construct in this case. The only reason I can give for using the other syntax is “too much cleverness”.