javascript / beginner
Snippet
Declaring Component Props with Fallback Values
In Svelte, component props are declared with the export keyword. Assigning a default value provides a safe fallback if the parent component does not supply the prop.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
<script>export let username = 'Guest';export let score = 0;</script><div class="user-badge"><p>User: {username}</p><p>Score: {Number(score)}</p></div>
svelte
Breakdown
1
export let username = 'Guest';
Declares an incoming prop named username with a default string fallback of 'Guest'.
2
export let score = 0;
Declares an incoming prop named score initialized to the number 0.