Svelte's `bind:`: Two-Way Data Binding Made Simple

Svelte's bind: turns one-way data flow into a two-way street, letting child elements update parent state. It's essential for syncing form inputs without manual event handlers. The footgun: an invalid number input yields undefined, not 0.
Why it exists
In UI development, you constantly need to sync a variable's value with a user-facing element, like a form input. The standard pattern is one-way data flow (state updates UI) plus an event handler (UI updates state). Svelte's bind: directive was created to eliminate the boilerplate of writing this event handler manually, simplifying this common UI task.
The mental model
Think of bind: as creating a direct, two-way link between a variable and an element's property. Normally, data flows down from your script to the template. bind: opens a second lane for data to flow back up from the template to your script whenever the user interacts with the element. It's syntactic sugar for a value prop and an update event listener rolled into one.
How it works
The Svelte compiler transforms a bind:property={variable} directive into the necessary code. For an <input>, it sets the input's value property and adds an on:input event listener. When the user types, the listener fires and updates the variable in your script. This reactivity then flows back down, ensuring the UI and state are always synchronized. The shorthand bind:value is identical to bind:value={value}.
When to use it
Use bind: extensively for form elements: bind:value for text/number inputs, bind:checked for checkboxes, and bind:group for radio button groups. It's also useful for media elements (e.g., bind:currentTime on a <video>), getting element dimensions (e.g., bind:clientWidth), and creating two-way bindings on your own component props.
When not to use it
Avoid bind: when you need complex logic or validation before updating state. While Svelte 5.9+ offers function bindings for this, a traditional value={...} prop paired with an on:input handler gives you more explicit control. Also, be aware of readonly bindings like clientWidth; you can only read from them, not set them.
One canonical example
The most common use case is syncing an input with a state variable. Consider this code: <script> let name = 'world'; </script> <input bind:value={name}> <h1>Hello {name}!</h1>. In this example, typing in the input field directly updates the name variable, which in turn updates the text in the <h1> tag. The data flows from user input, to script state, and back to the rendered output seamlessly.
Interview question
Which statement best describes the primary advantage of using Svelte's `bind:` directive?
- a.It allows direct manipulation of the DOM without needing Svelte's reactivity system.
- b.It simplifies two-way data synchronization by abstracting away manual event handling.Correct
- c.It enforces strict type checking on user input before state updates.
- d.It ensures that data only flows from the component's script to the UI element.
Why? this is the answer
The `bind:` directive's primary advantage is simplifying two-way data flow by combining a property assignment and an event listener, eliminating manual boilerplate. Option C is incorrect because while it handles invalid number input by setting `undefined`, it doesn't enforce strict type checking in a general sense.
Just read this? Test yourself on what you have been reading.
Read the original → svelte.dev
- #svelte
- #frontend
- #data-binding
- #reactivity
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on svelte — each one lists the topics its interview covers.
See open roles