It appears that it's a growing idiom in JS. It's likely adapted from the similar Perl idiom.
Neither JSLint nor JSHint like it.
I'm personally of two minds: it's super succinct and clever. Once you've see it once it's pretty obvious what's going on.
On the other hand, it is conflating expressions and statements. Turning an expression into flow-control.
I would use it in the same way I use in-line if blocks: only use if there's one statement and it all fits in one line. I probably wouldn't do this in a professional code base, but for my own stuff, sure.
In short, if it saves you an indent/block and can fit in one-line, it's okay. Other useful purposes are:
I like to read this as the "...if x AND then y" version of x && y
isWizard && hat = getPointyHat();
validInput || haltAndCatchFire(); // error validation, reads well "is validInput or error"
!valid && haltAndCatchFire(); // doesn't read as well but same logic
It should be used to make simple blocks that would otherwise be:
if (x) {
z = y();
}
into just x && z = y();
It shouldn't be used to handle any significantly complex logic or statements.
I'd say keep it limited to things like argument validations.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It appears that it's a growing idiom in JS. It's likely adapted from the similar Perl idiom.
Neither JSLint nor JSHint like it.
I'm personally of two minds: it's super succinct and clever. Once you've see it once it's pretty obvious what's going on.
On the other hand, it is conflating expressions and statements. Turning an expression into flow-control.
I would use it in the same way I use in-line if blocks: only use if there's one statement and it all fits in one line. I probably wouldn't do this in a professional code base, but for my own stuff, sure.
In short, if it saves you an indent/block and can fit in one-line, it's okay. Other useful purposes are:
I like to read this as the "...if x AND then y" version of
x && y
It should be used to make simple blocks that would otherwise be:
into just
x && z = y();
It shouldn't be used to handle any significantly complex logic or statements.
I'd say keep it limited to things like argument validations.