0
0
Svelteframework~20 mins

Input bindings (bind:value) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Input Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output when typing in the input?
Consider this Svelte component:
<script>
  let name = '';
</script>

<input bind:value={name} />
<p>Hello {name}!
What will the paragraph show after typing Anna in the input?
Svelte
<script>
  let name = '';
</script>

<input bind:value={name} />
<p>Hello {name}!</p>
AHello Anna!
BHello !
CHello {name}!
DHello undefined!
Attempts:
2 left
💡 Hint
The bind:value keeps the variable updated with the input's content.
state_output
intermediate
1:30remaining
What is the value of 'count' after clicking the button twice?
Given this Svelte component:
<script>
  let count = 0;
</script>

<input type="number" bind:value={count} />
<button on:click={() => count += 1}>Add 1</button>
<p>Count is {count}</p>

If the user clicks the button two times without changing the input manually, what will the paragraph show?
Svelte
<script>
  let count = 0;
</script>

<input type="number" bind:value={count} />
<button on:click={() => count += 1}>Add 1</button>
<p>Count is {count}</p>
ACount is 2
BCount is 0
CCount is 1
DCount is NaN
Attempts:
2 left
💡 Hint
The button increases the count variable by 1 each click.
📝 Syntax
advanced
2:00remaining
Which option correctly binds a checkbox to a boolean variable?
You want to bind a checkbox input to a boolean variable checked in Svelte. Which code snippet is correct?
A<input type="checkbox" bind:value={checked} />
B<input type="checkbox" bind:checked />
C<input type="checkbox" bind:checked={checked} />
D<input type="checkbox" bind:value checked={checked} />
Attempts:
2 left
💡 Hint
Checkboxes use bind:checked, not bind:value.
🔧 Debug
advanced
2:00remaining
Why does this input not update the variable?
Look at this Svelte code:
<script>
  let text = '';
</script>

<input bind:value=text />
<p>Text: {text}</p>

Typing in the input does not update the paragraph. What is the problem?
Svelte
<script>
  let text = '';
</script>

<input bind:value=text />
<p>Text: {text}</p>
AVariable 'text' is not declared
BMissing curly braces around 'text' in bind:value
CInput tag is missing a closing slash
DParagraph does not use {text} correctly
Attempts:
2 left
💡 Hint
bind:value expects an expression inside curly braces.
🧠 Conceptual
expert
2:30remaining
What happens when binding a number input to a string variable?
Consider this Svelte code:
<script>
  let age = '';
</script>

<input type="number" bind:value={age} />
<p>Age is {age} ({typeof age})</p>

If the user types 30 in the input, what will the paragraph show?
Svelte
<script>
  let age = '';
</script>

<input type="number" bind:value={age} />
<p>Age is {age} ({typeof age})</p>
AAge is NaN (number)
BAge is 30 (number)
CAge is 30 (undefined)
DAge is 30 (string)
Attempts:
2 left
💡 Hint
bind:value on number inputs still binds as string by default.