Svelte Special Tags

Svelte offers three special tags that go beyond plain expressions. One renders raw HTML, one creates a local constant inside a block, and one helps you inspect values while you debug. Each tag starts with an at sign inside curly braces.

The HTML Tag

Normal curly braces display text only. If a variable holds the string with bold tags, the page shows the tags as visible characters. The html tag tells Svelte to parse the string and insert real HTML elements.

<script>
  let notice = "Your plan renews on <strong>March 5</strong>.";
</script>

<p>{notice}</p>
<p>{@html notice}</p>

The first paragraph prints the tags as plain characters. The second paragraph shows March 5 in bold.

HTML Tag Flow Diagram

Text string with tags
        |
   +----+-----------------+
   |                      |
{notice}              {@html notice}
   |                      |
Shows raw characters   Builds real elements
<strong>March 5...     March 5 (bold)

Safety Rules For The HTML Tag

Svelte does not clean the string before it inserts the HTML. A malicious script hidden inside user-written text could run in the visitor's browser. This attack type is called cross-site scripting. Follow these rules to stay safe.

  • Use the html tag only for content you wrote or content a trusted sanitizer cleaned
  • Never pass comments, profile text, or form input straight into the html tag
  • Prefer normal curly braces whenever plain text is enough

A Layman Analogy

Normal curly braces work like a sealed gift box. Whatever sits inside stays inside, and nobody can open it by accident. The html tag opens the box and spills the contents onto the table. Open boxes from strangers only after a trusted person inspects them.

The Const Tag

The const tag creates a constant inside a block, such as an each block or an if block. It keeps repeated calculations tidy and gives them a readable name.

<script>
  let products = [
    { name: "Notebook", price: 40, quantity: 3 },
    { name: "Pen Set", price: 15, quantity: 5 }
  ];
</script>

{#each products as item}
  {@const lineTotal = item.price * item.quantity}
  <p>{item.name}: {lineTotal}</p>
{/each}

Each loop pass calculates its own lineTotal. The constant lives only inside the block, so nothing leaks into the rest of the component. The const tag must sit directly inside a block, an await clause, or a component with slot content.

The Debug Tag

The debug tag works like a console log that reacts to changes. List the variables you want to watch. Each time one of them changes, Svelte logs the new values to the browser console. When the browser developer tools stay open, the page pauses at that moment so you can inspect the state.

<script>
  let user = { name: "Ravi", age: 28 };
</script>

{@debug user}

<h3>{user.name}</h3>

The tag takes variable names only. It does not accept full expressions. Remove every debug tag before you publish the site, because visitors with open developer tools would trigger pauses.

Tag Comparison

Tag        Purpose                       Where It Sits
---------  ----------------------------  -------------------------
@html      Render trusted HTML strings   Anywhere in markup
@const     Name a value inside a block   Directly inside a block
@debug     Pause and log variables       Anywhere in markup

Common Mistakes To Avoid

  • Using the html tag on text typed by visitors
  • Placing the const tag outside a block, which causes a compile error
  • Writing an expression such as user.age + 1 inside the debug tag
  • Leaving debug tags in production code

Key Points

  • The html tag converts a string into real elements and skips any cleaning
  • The const tag creates a named constant inside a block
  • The debug tag logs and pauses when listed variables change
  • Safety always comes before convenience when handling HTML strings