---
description: 🚧 施工中
---

# eval has its own scope in strict mode

[JS](../../../../..) ⟩ [concepts](../../../../../../browser/concepts) ⟩ [strict mode]() ⟩ eval has its own scope in strict mode

{% hint style="success" %}
in [](<> "mention"), script in [eval](../../../../../scope/global/object/eval "mention") has its own [scope](../../../../../scope "mention").
{% 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: [ref](../../../../../err/ref "mention") ⟩ [not-defined](../../../../../err/ref/not-defined "mention")
{% endtab %}

{% tab title="👥 相關" %}
* [ref](../../../../../err/ref "mention") > [not-defined](../../../../../err/ref/not-defined "mention")
* [modify-current-scope-at-runtime](../sloppy-mode/modify-current-scope-at-runtime "mention")
{% 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 %}
