I found the following snippet when digging through some code:
"string" != typeof myVar && (myVar = "");
I understand what happens here. If myVar is not a string, that first condition evaluates to true so the second condition is evaluated, causing myVar to be set to "". So it's essentially a replacement for the following:
if ("string" != typeof myVar)
myVar = "";
The only discernible difference is that the former of the two strategies is also a return statement, though the code I found is not using the returned value. So I'm wondering:
- Are there any pros or cons to either strategy?
- If the only pro for the first strategy is the ability to return a value, is the snippet I found considered poor programming since it is more difficult to read?