What is the effect of strict mode on the with statement?

Strict mode prohibits the use of the with statement.

The with statement is disallowed in strict mode because it makes code optimization difficult and can lead to ambiguous code. In non-strict mode, the with statement is allowed, but it can cause unpredictable behavior and hinder performance optimizations.

// Non-strict mode
with (Math) {
  x = cos(2); // x is added to the global scope
}
"use strict";

// Strict mode
with (Math) { // SyntaxError: Strict mode code may not include a with statement
  x = cos(2);
}
More cards1
Show all