# lexical declaration cannot appear in a single-statement context❗️

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [error](https://lochiwei.gitbook.io/web/js/err) ⟩ [SyntaxError](https://lochiwei.gitbook.io/web/js/err/syntax) ⟩ lexical declaration cannot appear in a single-statement context

{% hint style="danger" %}
[<mark style="color:red;">**SyntaxError**</mark>](https://lochiwei.gitbook.io/web/js/err/syntax)<mark style="color:red;">**:**</mark> [<mark style="color:yellow;">**Lexical declaration cannot appear in a single-statement context**</mark>](https://lochiwei.gitbook.io/web/js/err/syntax/lexical-declaration-cannot-appear-in-a-single-statement-context):exclamation:
{% endhint %}

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

* replit：[can't use declaration when expecting statement](https://replit.com/@pegasusroe/cant-use-declaration-when-statement-expected#index.js)

```javascript
// ⭐ "if" syntax:
// -----------------------------------------------------
//                ╭──⭐───╮ <---- expecting a "statement"
// if (condition) statement;
// -----------------------------------------------------

//        ╭──var──╮      <---- ✅ "var" is a "statement"
if (true) var a = 1;

//        ╭── block ──╮  <---- ✅ "block" is a statement
if (true) { let b = 2 }

// -----------------------------------------------------------------
//     ❗can't use "declaration" when "statement" is expected❗
// -----------------------------------------------------------------
//
//        ╭──let──╮      <---- ❌ "let" is a "declaration"❗
if (true) let c = 2;
//        ^^^
//
// ⛔ SyntaxError: 
//    Lexical declaration cannot appear in a single-statement context
```

{% endtab %}

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

* [lexical-declaration](https://lochiwei.gitbook.io/web/js/grammar/declare/lexical-declaration "mention") - let/const/class.
* [var-is-a-statement](https://lochiwei.gitbook.io/web/js/variable/declare/var/var-is-a-statement "mention"):exclamation:
  {% endtab %}

{% tab title="✨ 範例" %}

* [statement-expected](https://lochiwei.gitbook.io/web/js/grammar/statement/statement-expected "mention"):exclamation:
  {% endtab %}

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

* [ ] [Difference between statements and declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#difference_between_statements_and_declarations) ⭐️⭐️⭐️ ❗️❗️❗️
* [ ] ECMA ⟩&#x20;
  * [ ] [Statement](https://tc39.es/ecma262/#prod-Statement)
  * [ ] [Declaration](https://tc39.es/ecma262/#prod-Declaration)
    {% endtab %}
    {% endtabs %}
