Rule
Don't place assignments inside conditionals. 
Mixing assignment and condition logic makes code error-prone and harder to understand. Separate assignments from logical checks. 
Supported languages:** JavaScript, TypeScript, Python, PHPIntroduction
Assignment operators inside conditional statements are a common source of bugs that compilers and linters often miss. The classic mistake is using = (assignment) instead of == or === (comparison) in an if statement, but the problem runs deeper. Even intentional assignments in conditionals create code that's difficult to read, review, and debug. When assignment and evaluation happen on the same line, readers must mentally parse which operation takes precedence and what value is actually being tested.
Why it matters
Why it matters
Bug introduction: A typo changing === to = won't cause a syntax error, just silently changes behavior. The condition evaluates to the assigned value (truthy/falsy), not the comparison result.
Code readability: Readers expect conditionals to test values, not modify them. When both happen simultaneously, maintainers must trace which variables are being modified and when.
Code examples
❌ Non-compliant:
function processUser(userData) {
    if (user = userData.user) {
        console.log(`Processing user: ${user.name}`);
        return user.id;
    }
    return null;
}
function validateInput(value) {
    if (result = value.match(/^\d{3}-\d{2}-\d{4}$/)) {
        return result[0];
    }
    return false;
}
Why it's wrong: The assignments inside conditionals make it unclear whether this is intentional or a typo. The first example could be a bug where === was intended, and the second mixes regex matching with assignment, making the code flow hard to follow.
✅ Compliant:
function processUser(userData) {
    const user = userData.user;
    if (user) {
        console.log(`Processing user: ${user.name}`);
        return user.id;
    }
    return null;
}
function validateInput(value) {
    const result = value.match(/^\d{3}-\d{2}-\d{4}$/);
    if (result) {
        return result[0];
    }
    return false;
}
Why this matters: Separating assignment from the conditional makes intent crystal clear. Readers immediately see that user is extracted first, then tested. The regex match result is captured, then evaluated. No ambiguity, no cognitive overhead, and typos like = vs === become obvious.
Conclusion
Keeping assignments separate from conditionals is a simple rule that prevents an entire class of bugs. The cognitive overhead of parsing combined operations outweighs any perceived brevity benefit. Clear, explicit code where assignment and evaluation are distinct operations improves readability, reduces bugs, and makes code review more effective.
.avif)
