#8518·svelte

Bug report: no error when using `@const` variable before initialization

Author: brandonmcconnellCreated Apr 18, 2023Updated Sep 27, 2026
Labelsbugcompiler

Describe the bug

I was trying to include a space after a comma in an if-statement. Consider this repro:

svelte
<script>
  const length = 10;
</script>

{#each { length } as _, i}
  {@const nth = i + 1}
  {nth}{#if nth !== length}, {/if}
{/each}

REPL

I did want the space to be a breaking space, so &nbsp; wouldn't work here. I tried &#32; which I believe is the equivalent code for a traditional breaking space, but that did not work either. (e.g. {nth}{#if nth !== length},&#32;{/if}).

Finally, I did something accidentally and reordered the two lines inside the enclosing {#each} block, only to find that change did achieve what I was trying to do even without a space or special char, but I feel like it shouldn't since the variable is now declared after it's used.

svelte
<script>
  const length = 10;
</script>

{#each { length } as _, i}
  {nth}{#if nth !== length},{/if}
  {@const nth = i + 1}
{/each}

REPL

Reproduction

REPL links above

Logs

No response

System Info

bash
macOS v13.1 (22C65)
Svelte v3.58.0 (in REPL)

Severity

annoyance