Javascript === ‘mint!’
1 min readSep 4, 2014
Consider this rather lengthy approach to error checking:
if (errors.length > 0) {
next(errors);
} else {
next();
}
Typically, you would use a ternary operator to express this more tersely:
next(errors.length > 0 ? errors : null);
However, this often becomes ugly and cumbersome with symbols littering themselves thought the code, for example:
next(errors.length > 0 ? errors.length < 10 ? errors : null);
My prefered approach is as follows:
next(errors.length && errors || null);
This uses short-circuit evaluation to return null if there are zero errors and implicit conversion to return the errors if there are any.