0
0
Svelteframework~10 mins

Component file (.svelte) anatomy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Component file (.svelte) anatomy
Start .svelte file
<script> block
Declare variables, functions
<style> block
Write CSS for component
HTML markup block
Use variables/functions in markup
Component ready to render
A .svelte file has three main parts: script for logic, style for CSS, and markup for HTML. They work together to create a component.
Execution Sample
Svelte
<script>
  let count = 0;
  function increment() { count += 1; }
</script>

<style>
  button { color: blue; }
</style>

<button on:click={increment}>Clicked {count} times</button>
This component shows a button that counts clicks using script, styles it blue, and updates the displayed count.
Execution Table
StepActionEvaluationResult
1Read <script> blockDeclare count=0 and increment functionVariables and functions ready
2Read <style> blockParse CSS for button colorButton styled blue
3Read markupCreate button element with click handlerButton displays 'Clicked 0 times'
4User clicks buttonCall increment(), count=0+1count updated to 1
5Re-render markupUpdate displayed countButton shows 'Clicked 1 times'
6User clicks button againCall increment(), count=1+1count updated to 2
7Re-render markupUpdate displayed countButton shows 'Clicked 2 times'
💡 User stops clicking, component stays with latest count
Variable Tracker
VariableStartAfter 1 clickAfter 2 clicksFinal
count0122
Key Moments - 3 Insights
Why does the count update on the button when I click it?
Because the increment function changes the count variable (see steps 4 and 6), which triggers Svelte to update the displayed count in the markup (steps 5 and 7).
What happens if I remove the <style> block?
The button will still work but lose the blue color styling applied in step 2, so it will show default button colors.
Can I put variables outside the <script> block?
No, variables and functions must be inside <script> so Svelte knows to track and update them (see step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count after step 4?
A2
B0
C1
Dundefined
💡 Hint
Check the 'Result' column at step 4 in the execution_table
At which step does the button text update to show the new count?
AStep 3
BStep 5
CStep 6
DStep 1
💡 Hint
Look for 'Re-render markup' action in the execution_table
If the <style> block is removed, what changes in the component?
AThe button loses blue color styling
BThe button text won't update
CThe button stops working
DThe count variable resets
💡 Hint
Refer to the key_moments about the