Convert && to if statement

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

Refactoring (Convert)
P42 for VS Code
P42 for GitHub
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();

becomes

anObject?.f();

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

condition && (myVariable = value);

becomes

if (condition) myVariable = value;

Configuration

Related Code Assists

Learn More

Available In