[JavaScript] Avoid unnecessary ternary statements

#1

Avoid unnecessary ternary statements, e.g., when falling back to a default value or coercing to a boolean:

Default value:

// good
var alwaysDefined = maybeFalsy || "defined";

// bad
var alwaysDefined = maybeFalsy ? maybeFalsy : "defined";

Boolean coercion:

// good
var isItTruthy = !!maybeFalsy;
var isItFalsy = !maybeFalsy;

// bad
var isItTruthy = maybeFalsy ? true : false;
var isItFalsy = !maybeFalsy ? true : false;
var isItFalsy = maybeFalsy ? false : true;

Reasoning: decreases verbosity; DRY (“Don’t Repeat Yourself”) in the case of default values

Reference: Airbnb JavaScript Style Guide, ESLint config no-unneeded-ternary

0 Likes