Lint Rules

noAssignInExpressions (since v12.0.0)

Disallow assignments in expressions.

In expressions, it is common to mistype a comparison operator (such as ==) as an assignment operator (such as =). Moreover, the use of assignments in expressions is confusing. Indeed, expressions are often considered as side-effect free.

Examples

Invalid

let a, b;
a = (b = 1) + 1;
nursery/noAssignInExpressions.js:2:6 lint/nursery/noAssignInExpressions  FIXABLE  ━━━━━━━━━━━━━━━━━━

   The assignment should not be in an expression.
  
    1 │ let a, b;
  > 2 │ a = (b = 1) + 1;
        ^^^^^
    3 │ 
  
   The use of assignments in expressions is confusing.
    Expressions are often considered as side-effect free.
  
   Suggested fix: Did you mean '==='?
  
    2 │ a·=·(b·===·1)·+·1;
          ++        
let a;
if (a = 1) {
}
nursery/noAssignInExpressions.js:2:5 lint/nursery/noAssignInExpressions  FIXABLE  ━━━━━━━━━━━━━━━━━━

   The assignment should not be in an expression.
  
    1 │ let a;
  > 2 │ if (a = 1) {
       ^^^^^
    3 │ }
    4 │ 
  
   The use of assignments in expressions is confusing.
    Expressions are often considered as side-effect free.
  
   Suggested fix: Did you mean '==='?
  
    2 │ if·(a·===·1)·{
         ++     
function f(a) {
    return a = 1;
}
nursery/noAssignInExpressions.js:2:12 lint/nursery/noAssignInExpressions  FIXABLE  ━━━━━━━━━━━━━━━━━

   The assignment should not be in an expression.
  
    1 │ function f(a) {
  > 2 │     return a = 1;
              ^^^^^
    3 │ }
    4 │ 
  
   The use of assignments in expressions is confusing.
    Expressions are often considered as side-effect free.
  
   Suggested fix: Did you mean '==='?
  
    2 │ ····return·a·===·1;
                ++   

Valid

let a;
a = 1;