"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.
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..
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.
It is a neat trick though, and proof that I need to spend a little more time hacking in javascript to improve my knowledge :-)
&& 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?