Convert && to if statement

Convert condition && aFunction(); and similar expression statements into if statements.

Convert && to if statement

Why is this refactoring helpful?

Using && short-circuiting to prevent code from being executed can be hard to understand. This practice is common when expressions are required, e.g., inside JSX. However, for regular statements, there are alternatives that are easier to understand, especially for developers who are less familiar with or experienced in JavaScript.

When checking that the leading part of an expression is not nullish, optional chaining is shorter and easier to understand:

anObject && anObject.f();



When testing a more general statement, an explicit if is often a more readable alternative:

condition && (myVariable = value);


if (condition) myVariable = value;


Related Code Assists

Learn More

Available In