Complete the code to create a simple Svelte component that displays a greeting.
<script> let name = "World"; </script> <h1>Hello, [1]!</h1>
The variable name holds the greeting target. Using {name} inside the HTML displays its value.
Complete the code to bind the input value to the name variable.
<script> let name = ""; </script> <input type="text" [1]={name} /> <p>Hello, {name}!</p>
value attribute which does not update the variable.bind or model.In Svelte, bind:value connects the input's value to the variable, updating it as the user types.
Fix the error in the component to correctly display the message only if name is not empty.
<script> let name = ""; </script> [1] (name) { <p>Hello, {name}!</p> }
if without Svelte block syntax.{#each} which is for loops, not conditions.Svelte uses {#if} blocks to conditionally render content. The syntax is {#if condition} ... {/if}.
Fill both blanks to create a button that increments a counter and displays it.
<script> let count = 0; function increment() { count [1] 1; } </script> <button on:click=[2]>Add 1</button> <p>Count: {count}</p>
-= which subtracts instead of adds.The operator += adds 1 to count. The button's click event calls the increment function.
Fill all three blanks to create a reactive statement that doubles the count and displays it.
<script> let count = 0; $: [1] = [2] * 2; </script> <button on:click=[3]>() => count += 1</button> <p>Double: {double}</p>
$: syntax.The reactive statement uses $: double = count * 2; to update double when count changes. The button increments count with an inline arrow function.