As such, the following comparisons all evaluate to True as they are all compared to their last evaluated expression.
console.log( ( true && true && "" ) === "" ); console.log( ( 0 && true && "" ) === 0 ); console.log( ( null || 0 || 3 ) === 3 ); console.log( ( null || 0 || "" ) === "" ); console.log( ( false || "blam" || true ) === "blam" ); console.log( ( true && null && true ) === null ); console.log( ( true && true && 16 ) === 16 ); console.log( ( "foo" || true || 16 ) === "foo" );
Ok, so how does this all tie back to AngularJS? In my case, this came up because I was using a compound ngSwitch condition in conjunction with a Boolean ngSwitchWhen case statement. My mistake, at the time, was assuming my switch condition would return a real Boolean value; but, given what I just talked about above, the switch condition does nothing more than return the last evaluated expression, which may or may not be a Boolean value.
Notice that in the first example the thumbnail URL is the last part of the compound condition. As such, while the non-empty string is a Truthy value, the switch never matches against the True case statement:
As you can see, the compound ngSwitch conditions worked when the last expression was a strict Boolean; but, that feels like it merely works by coincidence (at least in this case). As such, I would prefer either using the double-not operator to explicitly coerce the condition into a strict Boolean value; or, defer to a computed View-Model property that does the same.
Want to use code from this post? Check out the license.
You always explain things so well! :~) such soothing voice! gracias!
I think it does tend to do this where it can. For example, there is a directive - ng-if - that will implicitly coerce its input to a Boolean:
<div ng-if=" someTruthy "> ... </div>
... we get "Matched some string".
Here, the core switch operator did the same thing - our compound "truthy" condition simply returned the last evaluated expression, "some string", not "true".
So, I don't call this out as a bug - just something to be aware of.