Learn to read and maintain the language you have, because you can't win this.
&& 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?
"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 :-)
This is not really about whether semicolons are required or not.
The point is that in order to ensure maintainability of code you should try to use language (and framework) in a way which ensures better maintainability, supportability, and portability of your code.
Look, the statement like "a && b" is 100% valid in many languages but in order to increase maintainability, supportability, and portability of your code it should be written like "if (a) { b; }".
The easiest way to understand the point of this rule is to get a job maintaing some old crappy code-base :) - I learned that way.