# strict mode

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [concepts](https://lochiwei.gitbook.io/web/browser/concepts) ⟩ [environment](https://lochiwei.gitbook.io/web/js/concept/env) ⟩ [JS engine](https://lochiwei.gitbook.io/web/js/concept/env/js-engine) ⟩ [mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode) ⟩ strict

{% hint style="warning" %}
:shield: <mark style="color:red;">**always**</mark>**&#x20;**<mark style="color:yellow;">**use**</mark> [strict-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode "mention"):exclamation: (:point\_right: see [why](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode/always))
{% endhint %}

{% hint style="success" %}
a way to *opt in* to a <mark style="color:yellow;">**restricted variant**</mark> of JavaScript, thereby implicitly opting-out of "[sloppy-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/sloppy-mode "mention")".
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="danger" %} <mark style="color:red;">**in**</mark> <mark style="color:purple;">**strict mode**</mark>：

* [duplicate-parameter](https://lochiwei.gitbook.io/web/js/err/syntax/duplicate-parameter "mention"):exclamation:
* [function-in-block-fib](https://lochiwei.gitbook.io/web/js/val/func/declare/function-in-block-fib "mention") is <mark style="color:yellow;">**local**</mark> to the [block](https://lochiwei.gitbook.io/web/js/grammar/statement/other/block "mention").
  {% endhint %}

{% hint style="warning" %}
JS is <mark style="color:green;">**lenient**</mark> with [var](https://lochiwei.gitbook.io/web/js/variable/declare/var "mention"), <mark style="color:red;">**strict**</mark> with [let](https://lochiwei.gitbook.io/web/js/variable/declare/let "mention").

* [redeclare](https://lochiwei.gitbook.io/web/js/variable/declare/var/redeclare "mention"), even in [strict-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode "mention"):exclamation:
* [no-redeclare](https://lochiwei.gitbook.io/web/js/variable/declare/let/no-redeclare "mention"), even in [sloppy-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/sloppy-mode "mention"):exclamation:
  {% endhint %}

{% hint style="warning" %} <mark style="color:yellow;">**referencing**</mark> a [function-in-block-fib](https://lochiwei.gitbook.io/web/js/val/func/declare/function-in-block-fib "mention") <mark style="color:yellow;">**outside**</mark> the <mark style="color:yellow;">**block**</mark>：

* [sloppy-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/sloppy-mode "mention"): \
  ✅ [<mark style="color:yellow;">**hoisted**</mark>](https://lochiwei.gitbook.io/web/js/scope/hoist/function) / <mark style="color:yellow;">**initialized**</mark> to [undefined](https://lochiwei.gitbook.io/web/js/val/prim/undefined "mention") (in <mark style="color:orange;">**outer**</mark> [scope](https://lochiwei.gitbook.io/web/js/scope "mention"))
* [strict-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode "mention"): \
  ⛔ [<mark style="color:red;">**ReferenceError**</mark>](https://lochiwei.gitbook.io/web/js/err/ref/not-defined) (<mark style="color:yellow;">**invisible**</mark> in <mark style="color:orange;">**outer**</mark> [scope](https://lochiwei.gitbook.io/web/js/scope "mention")):exclamation:
  {% endhint %}

{% hint style="warning" %}
[class](https://lochiwei.gitbook.io/web/js/val/class "mention") <mark style="color:yellow;">**/**</mark> [es](https://lochiwei.gitbook.io/web/js/module/es "mention") <mark style="color:red;">**always**</mark> works in [strict-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode "mention")❗️
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}

* :white\_check\_mark:[always](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode/always "mention")
* [eval-has-its-own-scope-in-strict-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode/eval-has-its-own-scope-in-strict-mode "mention")
* [redeclare](https://lochiwei.gitbook.io/web/js/variable/declare/var/redeclare "mention") (<mark style="color:yellow;">**even in**</mark>**&#x20;**<mark style="color:purple;">**strict mode**</mark>):exclamation:
* [duplicate-parameter](https://lochiwei.gitbook.io/web/js/err/syntax/duplicate-parameter "mention") (<mark style="color:yellow;">**in**</mark>**&#x20;**<mark style="color:purple;">**strict mode**</mark>):exclamation:
  {% endtab %}

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

* :no\_entry:[delete-of-an-unqualified-identifier-in-strict-mode](https://lochiwei.gitbook.io/web/js/err/syntax/delete-of-an-unqualified-identifier-in-strict-mode "mention")
* :no\_entry:[octal-literals-not-allowed-in-strict-mode](https://lochiwei.gitbook.io/web/js/err/syntax/octal-literals-not-allowed-in-strict-mode "mention")
  {% endtab %}

{% tab title="🖥️ 影片" %}
{% embed url="<https://youtu.be/G9QTBS2x8U4>" %}
{% 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/)
* [ ] itHome ⟩ [你不可不知的 JavaScript 二三事](https://ithelp.ithome.com.tw/users/20112483/ironman/2016) ⟩&#x20;
  * [ ] [#Day11：Strict Mode (嚴謹模式)](https://ithelp.ithome.com.tw/articles/10205274)
  * [ ] [#Day12：看 Strict Mode 如何施展「還我漂亮拳」(1)](https://ithelp.ithome.com.tw/articles/10205275)
  * [ ] [#Day13：看 Strict Mode 如何施展「還我漂亮拳」(2)](https://ithelp.ithome.com.tw/articles/10205334)
    {% endtab %}

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

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