Avoid with Statement
ID |
javascript.avoid_with |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
best-practice, reliability |
Description
Reports with(obj) { … }. The with statement is forbidden in strict mode, blocks engine optimisations such as compile-time variable resolution, and produces ambiguous identifier binding — readers can’t tell whether foo inside the block refers to a local variable or to obj.foo.
Rationale
-
Throws a
SyntaxErrorin strict mode (which includes ES modules and class bodies by default). -
Defeats minifiers and JIT optimisations: every identifier inside the body has to be resolved dynamically against the with object.
-
Hides typos — a misspelled property silently falls through to the outer scope, where it might match an unrelated variable.
// Bad - banned in strict mode, optimisation hostile
with (config) {
enabled = true; // is this `config.enabled` or a free `enabled`?
}
Remediation
Use destructuring or a short alias instead.
// Good
const { enabled, threshold } = config;
// Or
const c = config;
c.enabled = true;