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"
})();
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 withlet
, so it is not a property of the global object.)This behavior is explained in the MDN documentation on eval.