InterviewHubPRO
Section Overview
Q307

Question 307 of 521

Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

59%
JavaScript Internals (521 Qs)•Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

Why do you need to avoid with statement

JavaScript's with statement was intended to provide a shorthand for writing recurring accesses to objects. So it can help reduce file size by reducing the need to repeat a lengthy object reference without performance penalty. Let's take an example where it is used to avoid redundancy when accessing an object several times.

javascript
  a.b.c.greeting = "welcome";
  a.b.c.age = 32;

Using with it turns this into:

javascript
  with (a.b.c) {
    greeting = "welcome";
    age = 32;
  }

But this with statement creates performance problems since one cannot predict whether an argument will refer to a real variable or to a property inside the with argument.