![]()
with Statement Causing Scope Confusion in JavaScript
The with statement in JavaScript is considered problematic because it can lead to scope confusion and unpredictable behavior. Due to these issues, it was deprecated in strict mode and is strongly discouraged in modern JavaScript development.
1. What is the with Statement?
The with statement was designed to simplify access to properties of a specific object. It temporarily adds an object to the scope chain, allowing you to omit the object reference when accessing its properties.
Syntax:
with (object) {
// Statements that access object properties
}
2. Example of Using with
Consider the following example:
const person = {
name: "Alice",
age: 25,
city: "New York"
};
with (person) {
console.log(name); // Alice
console.log(age); // 25
console.log(city); // New York
}
Here, inside the with (person) block:
nameis automatically resolved toperson.nameagebecomesperson.agecitybecomesperson.city
This may seem convenient, but it creates serious problems in JavaScript development.
3. Problems Caused by with
3.1 Scope Ambiguity and Performance Issues
The with statement dynamically modifies the scope chain, which can make variable resolution unpredictable and slow.
Example of Ambiguity:
const person = { name: "Alice" };
let name = "Bob";
with (person) {
name = "Charlie"; // Is this changing `person.name` or the outer `name`?
}
console.log(person.name); // "Charlie"
console.log(name); // "Bob"
Here, name = "Charlie" is ambiguous:
- If
nameexists inperson, it modifiesperson.name - If
namedoes not exist inperson, it modifies the outer variable
This ambiguity makes it hard to read and debug.
3.2 Issues in Strict Mode
In strict mode, the with statement is not allowed.
Example:
"use strict";
const person = { name: "Alice" };
with (person) {
console.log(name);
}
Error:
SyntaxError: Strict mode code may not include a with statement
Modern JavaScript (ES5 and later) enforces strict mode by default in many environments, making with completely unusable in such cases.
3.3 Potential for Silent Errors
Since with dynamically modifies scope, assigning a new variable inside a with block can create unintended global variables.
Example:
const person = { name: "Alice" };
with (person) {
newProperty = "Unexpected"; // Creates a global variable!
}
console.log(newProperty); // "Unexpected" (leaked into global scope!)
This is a huge risk because:
- Developers might accidentally create global variables, leading to memory leaks and security issues.
- It makes debugging harder, as unintended variables pollute the global scope.
4. The Recommended Alternative: Explicit Object Reference
Instead of using with, always refer to the object explicitly.
Using Direct Object References (Best Practice)
const person = { name: "Alice", age: 25, city: "New York" };
console.log(person.name); // Alice
console.log(person.age); // 25
console.log(person.city); // New York
This approach is: ✅ Clear and readable
✅ Faster (no dynamic scope changes)
✅ Works in strict mode
✅ Avoids accidental global variables
5. Modern Alternative: Object Destructuring
JavaScript destructuring allows for cleaner code without ambiguity.
const person = { name: "Alice", age: 25, city: "New York" };
const { name, age, city } = person;
console.log(name); // Alice
console.log(age); // 25
console.log(city); // New York
This method: ✅ Improves performance
✅ Reduces ambiguity
✅ Is recommended in ES6+ JavaScript
6. Conclusion: Avoid Using with
🔴 Never use the with statement because:
- It causes scope confusion.
- It leads to unpredictable behavior.
- It is forbidden in strict mode.
- It slows down performance.
- It increases the risk of creating global variables.
✅ Use object references or destructuring instead.
Key Takeaways:
✔ Always refer to object properties explicitly.
✔ Use ES6+ destructuring for cleaner syntax.
✔ Strict mode prevents with, making it obsolete.
✔ Modern JavaScript practices focus on clarity and performance.
Would you like an example of refactoring existing with code into a better approach?
