Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

with — legacy name-lookup statement

The with statement temporarily includes an object in name lookup. It is forbidden in strict mode; use explicit property access in new code.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

with (expression) statement

with is a reserved keyword. The statement with (expression) statement temporarily adds an object to the environment chain used for name lookup. After the statement executes, the previous environment is restored.

Lookup includes own and inherited properties, except names excluded by Symbol.unscopables. If the object does not expose a name, lookup continues in the outer environment. If a name resolves to a property through with, assignment targets that property rather than creating a local variable. Finding the property does not guarantee that the write succeeds: for example, outside strict mode, assignment to a non-writable data property can leave the object unchanged without throwing an error.

In strict mode, with causes a parse-time SyntaxError. Modules always use strict mode. The specification discourages this legacy construct: a name's meaning can depend on the object's properties. Prefer data.value or destructuring.

In the example, value += 1 changes data.value, while the outer value remains 10. Run the code as a classic script without "use strict", not as a module.

Examples

(function () {
  var value = 10;
  var data = { value: 20 };
  with (data) {
    value += 1;
  }
  console.log(data.value);
  console.log(value);
})();

Result

21
10

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources