Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two JavaScript statements into one

Tags:

javascript

Sometimes I want to combine two statements into one to omit curly braces in conditionals. For example, in PHP, instead of writing

if ($direction == 'south-east') {
    $x++;
    $y++;
    }

I would write

if ($direction == 'south-east') $x++ and $y++;

but is there a way to do that in JavaScript? Writing x++ && y++ doesn't seem to work.

like image 672
Sophivorus Avatar asked Dec 11 '22 23:12

Sophivorus


1 Answers

Curly braces aren't that bad, but - in very specific cases - you can enhance your code by using commas:

if (direction == 'south-east') x++, y++;

Here's the fiddle: http://jsfiddle.net/FY4Ld/

like image 88
Joseph Silber Avatar answered Dec 29 '22 13:12

Joseph Silber