IMO (in JS at least), binary operator use for non-binary are good if you know what you're dealing with.
consta=b||default;// Fallback to default if b is not set or falsy !!!consta=condition&&b;// Take the value of b when condition is truthyconsta=(condition&&b)||c;// Yay, one-line conditionnal// Take the value of b if truthy or fallback to c if falsy
I took care to write "falsy" and "truthy" because:
returndata.filter(something)||"fallback";// never get to fallback
But when filter is empty, it should have took "fallback" ?
Yes, but empty array [] is truthy.
But you should never care about the number of lines you write. Ninja coder can write this methods in 1 lines ? Whatever, I'll use 10 lines and have a clear code-base.
But in (condition && b) || c, if condition were true and b were false (or zero), it would return c. So that's not a correct implementation of a ternary operator.
Indeed, that's part of my point. You can do without ternary with some trickery, but best solution is to split it into real if else over multiple lines.
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.
IMO (in JS at least), binary operator use for non-binary are good if you know what you're dealing with.
I took care to write "falsy" and "truthy" because:
Yes, but empty array
[]
is truthy.But you should never care about the number of lines you write. Ninja coder can write this methods in 1 lines ? Whatever, I'll use 10 lines and have a clear code-base.
But in
(condition && b) || c
, ifcondition
were true andb
were false (or zero), it would returnc
. So that's not a correct implementation of a ternary operator.Indeed, that's part of my point. You can do without ternary with some trickery, but best solution is to split it into real
if else
over multiple lines.