[ACCEPTED]-How to combine two JavaScript statements into one-javascript
Curly braces aren't that bad, but - in very 1 specific cases - you can enhance your code by using commas:
if (direction == 'south-east') x++, y++;
Here's the fiddle: http://jsfiddle.net/FY4Ld/
{ }
that you use does just that - combines 4 statements in one block usable with any 3 flow control statement. You can place entire 2 block on one line - JS doesn't care about 1 whitespace in this case.
You can do it like this:
var x = 0,
y = 0,
cond = true;
if (cond) ++x && ++y;
Note: This doesn't 5 work if x is -1
;
Or without if
:
cond && ++x && ++y;
So your code would 4 look like this:
if ($direction == 'south-east') $x++ && $y++;
Note that the ++
operators 3 is after the variable that means this won't 2 work if $x
is 0
.
++x
returns falsy if x is -1
x++
returns 1 falsy if x is 0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.