no-multi-assign

Disallow chained assignments such as a = b = c

In const width = height = 100, only width is declared. height is a plain assignment. It throws in strict mode if nothing declares it, and creates a global variable otherwise. Even when every name is declared, the second assignment is easy to overlook.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-multi-assign" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-multi-assign" = ["error", {
  ignoreNonDeclaration = false,
}]

Examples#

example.js1 problem
const width = height = 100;
example.js1 problem
let first, last;
first = last = items[0];
example.js1 problem
class Box {
  width = this.height = 100;
}
example.jsNo problems
const width = 100;
const height = 100;

let first, last;
first = items[0];
last = first;

Options#

OptionDefaultWhat it does
ignoreNonDeclarationfalseOnly report chains in a var, let or const declaration or a class field. Allow them between variables that already exist.
example.jswith { "ignoreNonDeclaration": true }No problems
let first, last;
first = last = items[0];
example.jswith { "ignoreNonDeclaration": true }1 problem
const width = height = 100;