Direct call and indirect call of `eval` behave differently
Author: wheatupCreated Jun 26, 2024Updated Sep 1, 2024
Labelsnew-example
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.
Source: denysdovhan/wtfjs