> 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/grammar/statement/statement-expected.md).

# statement expected❗️

can't use "declaration" when expecting "statement"❗️

[JS](/web/js.md) ⟩ [grammar](/web/js/grammar.md) ⟩ expecting statement

{% hint style="danger" %} <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**use**</mark> [declaration](/web/js/grammar/declare.md) when <mark style="color:yellow;">**expecting**</mark> [statement](/web/js/grammar/statement.md):exclamation:
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
[statement vs. declaration](/web/js/grammar/vs-declare.md)

* [var](/web/js/variable/declare/var.md) is a [statement](/web/js/grammar/statement.md):exclamation:  (👉 [var is a statement❗️](/web/js/variable/declare/var/var-is-a-statement.md))
* [let](/web/js/variable/declare/let.md)/[const](/web/js/variable/declare/const.md) is a [declaration](/web/js/grammar/declare.md). &#x20;
  {% endhint %}
  {% endtab %}

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

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

{% 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 %}

```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="📘 手冊" %}

* [ ] [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 %}
