Javascript === ‘mint!’

InfectiousGrin

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.

    InfectiousGrin

    Written by

    http://infectiousgr.in

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade