What is a Svelte Action? Write a simple logging action.

Whether you understand a Svelte action as a lifecycle function attached to a DOM node via use: and can write its minimal signature.
Define a function receiving the node, log a message, and optionally return destroy.
What's really being asked
This question checks whether you know the difference between declarative template logic and imperative DOM manipulation in Svelte. An action is the escape hatch for when you need to touch the actual DOM node directly. The interviewer wants to see that you understand the use directive, the function signature, and the lifecycle timing. Specifically, they are looking for awareness that the action runs when the element is created and mounted, not during compilation or at the module level.
The full answer
A strong answer hits four things in order. First, state that an action is a plain function that receives the DOM element as its first argument. Second, explain that you attach it to an element in the template with the use directive, such as use:myAction. Third, show that the function body runs immediately when the node enters the DOM, which is the right place for side effects like logging or integrating non-Svelte libraries. Fourth, mention the optional return object that can contain an update method for reactive parameter changes and a destroy method for cleanup when the element is removed.
The mistakes people make
Red flags include confusing actions with onMount or other component lifecycle hooks, since actions are element-level rather than component-level. Another mistake is treating the action like an event handler that returns a cleanup function directly instead of an object with a destroy key. Some candidates forget that the action receives the raw DOM node and try to return Svelte markup from it. Also, logging outside the function body or putting the action definition inside a reactive statement shows a misunderstanding of when the code executes.
What usually comes next
The interviewer might ask how you would pass parameters to an action and how the update method differs from re-running the whole action. They could also ask when you would choose an action over a reactive effect in Svelte 5, or how to type an action with TypeScript using the Action interface from svelte/action. Another common follow-up is how you would handle cleanup for global event listeners or IntersectionObservers inside an action.
A concrete example
You could write a simple action in a file called logger.js. The function is named logOnMount and takes node as its argument. Inside the function, you run console.log with the message Element mounted and the node itself. Then in a Svelte component, you import the action and apply it to a div element using the use:logOnMount directive. When the component renders, the browser console shows the message immediately after the div enters the DOM. If you want cleanup, you return an object with a destroy method that runs another console.log when the element is removed.
Interview question
Which approach correctly implements a minimal Svelte action that logs when an element is mounted?
- a.Write a function that logs immediately at the module level and attach it in the template with the on directive.
- b.Use an onMount hook in the component script to query the DOM and log the element.
- c.Write a function that receives the node, logs a message, returns a cleanup function directly, and attach it with the use directive.
- d.Write a function that receives the DOM node, logs inside the function body, and is attached in the template with the use directive.Correct
Why? this is the answer
A Svelte action is attached via the use directive and receives the raw DOM node to run side effects immediately upon mounting. Option C is tempting because it uses the correct directive and node argument, but it incorrectly returns a standalone cleanup function instead of an object with a destroy key.
Just read this? Test yourself on what you have been reading.
Read the original → svelte.dev
- #svelte
- #actions
- #dom
- #lifecycle
- #frontend
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