Introduction
Using data in pages lets you show information that can change or come from outside. It makes your page useful and interactive.
Using data in pages lets you show information that can change or come from outside. It makes your page useful and interactive.
<script> let message = 'Hello, world!'; </script> <p>{message}</p>
{} curly braces to insert data into HTML.<script> tag to use them in the page.<script> let name = 'Alice'; </script> <h1>Welcome, {name}!</h1>
<script> let count = 5; </script> <p>You have {count} new messages.</p>
<script> let items = ['Apple', 'Banana', 'Cherry']; </script> <ul> {#each items as item} <li>{item}</li> {/each} </ul>
This page shows user information by using data stored in an object.
<script> let user = { name: 'Sam', age: 30 }; </script> <main> <h2>User Info</h2> <p>Name: {user.name}</p> <p>Age: {user.age}</p> </main>
Data inside <script> is reactive. Changing variables updates the page automatically.
Use {#each} blocks to loop over arrays and show lists.
Keep data simple and clear for easy updates and maintenance.
Use variables inside <script> to hold data.
Insert data into HTML with curly braces {}.
Use loops like {#each} to show lists dynamically.
{} to insert JavaScript expressions or variables into HTML.{variable} inside HTML shows the variable's value dynamically.{variable} inside the HTML. -> Option B<script> block?<script>, so let is the preferred way to declare variables that can change.let name = 'Svelte'; correctly declares a variable with a string value.let name = 'Svelte'; -> Option C<script>
let fruits = ['Apple', 'Banana', 'Cherry'];
</script>
<ul>
{#each fruits as fruit}
<li>{fruit}</li>
{/each}
</ul>fruits and creates an <li> for each item.<li>, so the output is a list with three items.<script>
let count = 0
</script>
<button on:click={count++}>Click me</button>
<p>Clicked {count} times</p>count++ inside on:click is valid and works as an event handler that increments count.count and displays the updated value.const users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Carol', age: 35 }
];<div> using {#each}?{#each users as user, index} correctly loops over users and provides both user object and index.{user.name} and {user.age} correctly access properties. Including index shows user number.{#each users as user, index}
<div>User {index}: {user.name} ({user.age})</div>
{/each} [OK]Pass the test and this pattern moves unknown