> 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/err/syntax/lexical-declaration-cannot-appear-in-a-single-statement-context.md).

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

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

{% hint style="danger" %}
[<mark style="color:red;">**SyntaxError**</mark>](/web/js/err/syntax.md)<mark style="color:red;">**:**</mark> [<mark style="color:yellow;">**Lexical declaration cannot appear in a single-statement context**</mark>](/web/js/err/syntax/lexical-declaration-cannot-appear-in-a-single-statement-context.md):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](/web/js/grammar/declare/lexical-declaration.md) - let/const/class.
* [var is a statement❗️](/web/js/variable/declare/var/var-is-a-statement.md):exclamation:
  {% endtab %}

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

* [statement expected❗️](/web/js/grammar/statement/statement-expected.md):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 %}
