close

no-cond-assign

Configuration

PresetConfigured Value
✅ js.configs.recommended"error"
✅ ts.configs.recommended"error"
rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-cond-assign': 'error',
    },
  },
]);

Rule Details

Disallows assignment operators in conditional expressions (if, while, do-while, for, and ternary). Assignments in conditional statements are frequently a typo where the developer meant to use a comparison operator (===) instead of an assignment operator (=).

In the default "except-parens" mode, assignments are allowed if they are wrapped in extra parentheses, which signals the assignment is intentional. In "always" mode, all assignments in conditionals are flagged.

Examples of incorrect code for this rule:

if ((x = 0)) {
}

while ((x = next())) {}

var result = x ? (y = 1) : z;

Examples of correct code for this rule:

if (x === 0) {
}

while ((x = next())) {} // extra parens signal intent

if (x === 0 || (y = getValue())) {
}

for (; (a = b); ) {}

Original Documentation