JavaScript with Statement
The with statement is deprecated and is not allowed in strict mode. Use destructuring instead.
The deprecated with statement adds an object's properties to the scope of a block. It is forbidden in strict mode and shouldn't be used.
Description
The with statement extends the scope chain for a block, so the object's properties can be used like variables. It makes code ambiguous (is x a property or a variable?), slows down engines and is not allowed in strict mode, modules or classes. Use destructuring, const { a, b } = obj, to get short names for properties instead.
Inside a with block, JavaScript looks up each name first as a property of the object and only then in the surrounding scopes. That means adding a property to the object later can silently change which variable a piece of code refers to, which makes bugs very hard to find. It also prevents engines from optimizing the code, because names can't be resolved in advance.
Because modules and classes are always strict, with can't be used in modern code at all. Destructuring the properties you need into local variables, or simply keeping a short variable name for the object, gives the same convenience without the ambiguity.
Syntax
with (object) {
statement
}Example
Browser Support
The following information will show you the current browser support for the with statement. Hover over a browser icon to see the version that first introduced support for this statement.
Desktop





Tablets & Mobile





