CodeZettaInterview Hub

JavaScript · Execution Context · Senior · Architecture

How do `eval` and `with` affect execution contexts and scope, and why are they discouraged?

Short Interview Answer

They can dynamically introduce or alter bindings in the current (or new) environment, defeating static scope analysis and hurting optimization and security.

Detailed Explanation

Direct `eval` in non-strict mode can add variables into the calling VariableEnvironment, effectively mutating the current execution context's bindings at runtime. `with` extends the scope chain with an object, so property lookups become dynamic identifier lookups. Both break lexical predictability: engines cannot optimize identifier resolution as aggressively, tooling cannot analyze code statically, and `eval` of untrusted input is a serious XSS/injection risk. Strict mode restricts `eval` (creates its own declarative environment) and forbids `with`. In modern codebases, prefer Function constructors only when necessary, and never `with`.

Interview Tip

Frame the answer around static analyzability and security — that signals senior-level judgment.

Common Mistake

Only saying 'eval is slow' without explaining the scope/execution-context mutation and security angle.

Did you know this answer?