Skip to content

Direct call and indirect call of eval behave differently #327

Open
@wheatup

Description

@wheatup

When calling eval indirectly, it uses the top scope. This means you can access the top scope variables even in a local scope where the same variable name is redefined. (Note that the variable is defined with let, so it is not a property of the global object.)

let foo = 'outer';

(() => {
  let foo = 'inner';

  console.log(eval('foo'));  // > "inner"
  console.log(eval?.('foo'));  // > "outer"
})();

This behavior is explained in the MDN documentation on eval.

Metadata

Metadata

Assignees

No one assigned

    Labels

    new-exampleA proposal of the new example

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions