4 ms·
Okay, I'm going to bite. && is logical AND, and it operates on boolean expressions (or types). So "a && b" evaluates to true if both a and b are true. How ca
by radicalbyte 15y ago
Okay, I'm going to bite.
&& is logical AND, and it operates on boolean expressions (or types).
So "a && b" evaluates to true if both a and b are true.
How can that be replaced by "if(a){b;}"?
Is it "r = a && b" being replaced by "if(a){r=b}"?
Using Chrome's javascript console:
> a = "asdas"; b="da"; a == b;
false
> a = "asdas"; b="da"; if(a) { b };
"da"
Could you (or some other javascript expert) explain it to me?
- ajross 15y agoThe && operator short circuits. It evaluates its second operand only if the first is true (becuase if the first is false, then the expression result is known to be false). It thus acts pretty much exactly like "if", but with an infix syntax. This trick is used pervasively in shell programming, where the native if syntax sucks.
- radicalbyte 15y agoThanks, I get it now :) So the second operand can be a function / method, with any (or no) return type. > function bob () { alert("bob") }; true && bob(); Alerts "bob". Nice trick, though not something I'd personally ever us as it's not concise and isn't portable to C# (and I assume Java/C++). Plus using the IF statement is a better showing of intent.. and I don't really like relying on job security through obscurity..
- ajross 15y agoIt is concise. There are fewer tokens and bytes needed to write A && B than if ( A ) { B }. And the intent thing is, as I've tried to explain, very much situation-specific. There are many programmers out there very comfortable with this idiom. That it doesn't happen to be popular in the web development community doesn't mean it sucks or isn't worth learning. Don't pass judgement on syntax you literally just learned, basically. Don't assume the world you know is the only one worth knowing. Good hackers know their tools.
- radicalbyte 15y agoI'm not judging it, I'm just saying that it's not something I'd use because it's confusing for my colleagues / anyone maintaining my work. Unless they're a pure Javascript person. It is a neat trick though, and proof that I need to spend a little more time hacking in javascript to improve my knowledge :-)
- bzbarsky 15y agoIn JavaScript, && does not evaluate to a boolean. It evaluates to the thing on the left if that thing is false (in which case the right hand side is not evaluated at all), otherwise to the thing on the right. So in said JavaScript console, "asdas" && "da" evaluates to "da" and "" && "da" evaluates to "". So in an assignment context |r = a && b| would need to be replaced by: if (a) { r = a } else { r = b }, which may or may not be more confusing than the original statement with &&. But in a statement context, where the result of && is not being assigned to anything at all, "a && b" is exactly the same as "if (a) b;" except harder to understand.
- artsrc 15y agoGoing the other way: In languages where if is an expression if (a) { b; } would return b if a was true and nothing otherwise. Which is a similar to a && b.