Skip to main content

js/noCondAssign

disallow assignment operators in conditional expressions

ESLint Equivalent: no-cond-assign

Examples

Invalid

for (let i = 1; i = 10; i++) {
	console.log('foo');
}

 file.ts:1:16 lint/js/noCondAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not assign variables in loop conditions.

  > 1for (let i = 1; i = 10; i++) {
                     ^^^^^^
    2  console.log('foo');
    3}

  It is a common typo to mistype an equality operator as an assignment
    operator.


if (foo = 'bar') {
	console.log('foo');
}

 file.ts:1:4 lint/js/noCondAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not assign variables in loop conditions.

  > 1if (foo = 'bar') {
         ^^^^^^^^^^^
    2  console.log('foo');
    3}

  It is a common typo to mistype an equality operator as an assignment
    operator.


while (foo = 'bar') {
	console.log('foo');
}

 file.ts:1:7 lint/js/noCondAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not assign variables in loop conditions.

  > 1while (foo = 'bar') {
            ^^^^^^^^^^^
    2  console.log('foo');
    3}

  It is a common typo to mistype an equality operator as an assignment
    operator.


do {
	console.log('foo');
} while (foo = 'bar')

 file.ts:3:9 lint/js/noCondAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not assign variables in loop conditions.

    1do {
    2  console.log('foo');
  > 3} while (foo = 'bar')
              ^^^^^^^^^^^

  It is a common typo to mistype an equality operator as an assignment
    operator.


(foo = bar) ? foo() : bar();

 file.ts:1:1 lint/js/noCondAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not assign variables in loop conditions.

    (foo = bar) ? foo() : bar();
     ^^^^^^^^^

  It is a common typo to mistype an equality operator as an assignment
    operator.

Valid

while ((foo = foo.bar) !== undefined) {
	console.log(foo);
}
if (foo++ === 3) {
	console.log(foo);
}
foo = bar ? foo() : bar();