JavaScript With Statement


JavaScript ‘with’ keyword is unreliable and expensive and should be avoided.

With statement builds a new local scope with the parameter object. Everything in object is treated as local to the ‘with’ block.

Building new properties on the fly doesn’t work as expected. It will create a global.

The goal of ‘with’ is to reduce redundancy. A better alternative is to cache the object in a variable to avoid lengthy nested object names.