Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Displaying Data with Curly Braces | Components and Props
Introduction to Svelte

Displaying Data with Curly Braces

Swipe to show menu

One of the most important features of Svelte is the ability to display dynamic data directly inside the user interface. Instead of hardcoding values into HTML, you can connect the interface to JavaScript variables and update content automatically.

Svelte uses curly braces { } to render JavaScript values inside components.

Rendering Dynamic Values

Open the App.svelte file and add the following code:

<script>
  let title = "Learning Svelte";
  let lessons = 12;
</script>

<h1>{title}</h1>

<p>Total lessons: {lessons}</p>

The values stored inside the variables appear directly inside the interface.

How Curly Braces Work

Curly braces allow Svelte to insert JavaScript expressions into HTML.

For example:

<p>{lessons}</p>

Svelte reads the value of the lessons variable and displays it inside the paragraph element.

This works with:

  • Strings;
  • Numbers;
  • Variables;
  • Expressions;
  • Function results.

Using Expressions

You can also place simple JavaScript expressions inside curly braces.

<script>
  let price = 20;
</script>

<p>Total: ${price * 2}</p>

Svelte evaluates the expression and displays the result inside the interface.

Updating the UI Automatically

One of the biggest advantages of Svelte is automatic UI updates.

If the value of a variable changes, the displayed content updates automatically without manually editing the DOM.

This behavior is part of Svelte's reactive system.

question mark

What do curly braces { } do in a Svelte component?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 2. Chapter 1
some-alt