JS Coding Questions Logo
JS Coding Questions
#303💼 Interview💻 Code

Why do you need to avoid with statement

Advertisement

728x90

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
1a.b.c.greeting = "welcome";
2
3a.b.c.age = 32;

Using with it turns this into:

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

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.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 47

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
303of476