> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode/eval-has-its-own-scope-in-strict-mode.md).

# eval has its own scope in strict mode

[JS](/web/js.md) ⟩ [concepts](/web/browser/concepts.md) ⟩ [strict mode](/web/js/concept/env/js-engine/mode/strict-mode.md) ⟩ eval has its own scope in strict mode

{% hint style="success" %}
in [strict mode](/web/js/concept/env/js-engine/mode/strict-mode.md), script in [eval](/web/js/scope/global/object/eval.md) has its own [scope](/web/js/scope.md).
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}

* replit：[modify scope at runtime](https://replit.com/@pegasusroe/modify-scope-at-runtime#index.js)

```javascript
function evalCannotModifyScopeInStrictMode() {
    
    'use strict';    // ⭐️ strict mode
    
    // ---------------------------------------------------------------
    // ⭐️ in strict mode, `eval` doesn't introduce new variables into
    //    the surrounding scope, they're local to the script itself.
    eval("var b = 'bbb!';");
    //        ╰── `b` is "local" to the inner script❗
    // ---------------------------------------------------------------
    
    console.log(b);    // `b` is invisible.
    //          ^
    // ⛔ ReferenceError: `b` is not defined
}

evalCannotModifyScopeInStrictMode();   // ⛔ ReferenceError (runtime error)
```

:point\_right: [⛔️ ReferenceError](/web/js/err/ref.md) ⟩ ['xxx' is not defined❗️](/web/js/err/ref/not-defined.md)
{% endtab %}

{% tab title="👥 相關" %}

* [⛔️ ReferenceError](/web/js/err/ref.md) > ['xxx' is not defined❗️](/web/js/err/ref/not-defined.md)
* [modify current scope at runtime❗️](/web/js/concept/env/js-engine/mode/sloppy-mode/modify-current-scope-at-runtime.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] &#x20;[`eval` of strict mode code does not introduce new variables into the surrounding scope](https://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
  {% endtab %}
  {% endtabs %}
