JavaScript The with Statement

The with statement is used to extend the scope chain for a block of code. It allows you to add objects’ properties to the current scope, so you can access those properties without using the object name repeatedly. While it can be useful in some cases, it is generally considered bad practice in modern JavaScript due to potential issues with readability, maintainability, and the possibility of variable conflicts.

1. Syntax of with Statement:

with (object) {
    // Code to execute using the properties of the object
}

Try It Now

Explanation:

  • The with statement extends the scope chain to include the properties of the specified object.
  • Inside the with block, you can refer to the properties of the object without explicitly using the object name.

2. Example of with Statement:

let person = {
    name: "John",
    age: 30
};

with (person) {
    console.log(name); // John
    console.log(age);  // 30
}

Try It Now

Explanation:

  • The with statement allows you to directly access name and age properties of the person object without needing to reference person.name or person.age.

3. Why is the with Statement Deprecated?

Despite its convenience, the with statement is problematic for several reasons:

  1. Ambiguity: It can lead to confusing code when variables or properties of the object shadow existing variables in the scope.
  2. Performance: The with statement can reduce performance as the JavaScript engine needs to evaluate the object properties for each statement inside the with block.
  3. Readability: It makes the code harder to read and maintain since it is not immediately clear where a variable or property is coming from.

For these reasons, the use of with is strongly discouraged in modern JavaScript. Most linting tools will flag the with statement as an error, and it is not allowed in strict mode ('use strict';).

4. Example of Problems with with:

let person = {
    name: "Alice",
    age: 25
};

let name = "Bob";

with (person) {
    console.log(name); // Bob
    console.log(age);  // 25
}

Try It Now

Explanation:

  • The name variable inside the with block is ambiguous because it can refer to both the name property of the person object and the name variable in the outer scope. In this case, it refers to the variable name ("Bob"), not the object property.

5. Alternatives to with:

Instead of using with, it’s better to directly reference object properties or destructure the object for easier access.

Using Direct Property Access:

let person = {
    name: "Alice",
    age: 25
};

console.log(person.name); // Alice
console.log(person.age);  // 25

Try It Now

Using Object Destructuring:

let person = {
    name: "Alice",
    age: 25
};

let { name, age } = person;
console.log(name); // Alice
console.log(age);  // 25

Try It Now

Conclusion:

  • The with statement allows you to work with an object’s properties without referencing the object each time, but it is not recommended in modern JavaScript due to potential ambiguity and performance issues.
  • Use alternatives like direct property access or object destructuring for more reliable and maintainable code.