Svelte / SvelteKit interview questions

25 questions from the Svelte / SvelteKit course, each with the correct answer and an explanation. Try to answer before you open the answer.

Svelte Foundations

1. How does Svelte update the DOM when state changes?

  1. It re-renders the whole page from the server
  2. It diffs a virtual DOM tree on every change
  3. Compiled code updates only the nodes that depend on the changed state
  4. It reloads the component file in the browser
Show answer

Answer: Compiled code updates only the nodes that depend on the changed state

Svelte is a compiler; the work is decided at build time.

Lesson: What Svelte Is

2. Where do you configure the SvelteKit deployment adapter?

  1. In svelte.config.js under kit.adapter
  2. In src/app.html
  3. In +page.svelte
  4. In package.json under "adapter"
Show answer

Answer: In svelte.config.js under kit.adapter

svelte.config.js holds compiler and kit options.

Lesson: Creating a Project With sv

3. What does Svelte do to a p { } rule in a component style block?

  1. Moves it into an inline style attribute
  2. Applies it to every paragraph in the app
  3. Ignores it unless marked :global
  4. Scopes it to that component with a generated class
Show answer

Answer: Scopes it to that component with a generated class

Component styles are scoped by default.

Lesson: Anatomy of a Component

Runes and Reactivity

4. What is the main difference between $state and $state.raw for an array?

  1. $state.raw is not reactive at all
  2. $state.raw only updates on reassignment, not on mutation
  3. $state.raw makes the array deeply reactive
  4. $state.raw can only hold strings
Show answer

Answer: $state.raw only updates on reassignment, not on mutation

Raw state skips the deep proxy.

Lesson: $state

5. When should you use $derived.by instead of $derived?

  1. When the value must be writable from outside
  2. When the computation needs several statements
  3. When the value should never update
  4. When you need to run a side effect
Show answer

Answer: When the computation needs several statements

$derived.by takes a function body.

Lesson: $derived

6. Which is the best tool to compute fullName from firstName and lastName state?

  1. A setInterval that polls the values
  2. $effect that assigns to a $state variable
  3. $effect.pre
  4. $derived
Show answer

Answer: $derived

Derive state; reserve effects for side effects.

Lesson: $effect and When Not to Use It

Components and Composition

7. How does a Svelte 5 component declare a prop with a default value?

  1. let { size = 16 } = $props();
  2. export let size = 16;
  3. const size = $state.prop(16);
  4. props.size ||= 16;
Show answer

Answer: let { size = 16 } = $props();

Svelte 4 used export let; Svelte 5 uses $props.

Lesson: $props

8. What replaces createEventDispatcher in Svelte 5?

  1. A global event bus store
  2. The on: directive on components
  3. Passing callback functions as props
  4. The $emit rune
Show answer

Answer: Passing callback functions as props

Component events are callback props.

Lesson: Callback Props, $bindable and bind:

9. Content placed between `<Card>` and `</Card>` is available to Card as...

  1. A string prop called content
  2. A $slots object
  3. The children snippet prop, rendered with {@render children()}
  4. The default export of the component
Show answer

Answer: The children snippet prop, rendered with {@render children()}

Snippets replace the default slot.

Lesson: Snippets and {@render}

Template Logic, Bindings and Motion

10. Why add (item.id) to an {#each} block?

  1. So Svelte can match DOM nodes to items when the list is reordered or changed
  2. To sort the list by id
  3. To make the list load faster from the server
  4. It is required syntax for every each block
Show answer

Answer: So Svelte can match DOM nodes to items when the list is reordered or changed

Keys give list items identity.

Lesson: {#if}, {#each} and {#await}

11. What should an action return to clean up listeners when the element is removed?

  1. Nothing; Svelte removes listeners automatically
  2. A Promise
  3. A $state value
  4. An object with a destroy method
Show answer

Answer: An object with a destroy method

Actions control their own setup and teardown.

Lesson: Element Bindings and Actions

12. Which requirement applies to animate:flip?

  1. It only works inside {#await}
  2. It must be on an element directly inside a keyed each block
  3. It needs a global CSS animation
  4. It only animates opacity
Show answer

Answer: It must be on an element directly inside a keyed each block

flip relies on keys to track positions.

Lesson: Transitions and Animations

Sharing State

13. Why is a module-level $state singleton risky in a SvelteKit app?

  1. Runes do not work in modules
  2. During SSR it is shared by all requests on the server
  3. It cannot be imported by components
  4. It is reset on every click
Show answer

Answer: During SSR it is shared by all requests on the server

Server modules live as long as the process.

Lesson: State in .svelte.js and .svelte.ts Modules

14. Where must getContext be called?

  1. Only inside +server.ts files
  2. Inside an onclick handler
  3. Inside a setTimeout callback
  4. At the top level of a component script during initialisation
Show answer

Answer: At the top level of a component script during initialisation

Context is tied to component creation.

Lesson: The Context API

15. What does $count do inside a component when count is a store?

  1. Subscribes to the store and unsubscribes automatically on destroy
  2. Creates a new $state copy that never updates
  3. Reads the value once at startup only
  4. Converts the store into a derived rune
Show answer

Answer: Subscribes to the store and unsubscribes automatically on destroy

The $ prefix is store auto-subscription.

Lesson: Stores and When They Still Help

SvelteKit Routing and Loading

16. Which path serves /blog/my-post?

  1. src/pages/blog/[slug].svelte
  2. src/routes/blog/slug.svelte
  3. src/routes/blog/[slug]/+page.svelte
  4. src/routes/blog/+slug.svelte
Show answer

Answer: src/routes/blog/[slug]/+page.svelte

Brackets mark a dynamic segment.

Lesson: Filesystem Routes and Layouts

17. Where should a load function that queries the database with a secret key live?

  1. In +page.svelte
  2. In +page.ts
  3. In +page.server.ts
  4. In src/app.html
Show answer

Answer: In +page.server.ts

Universal loads also run in the browser.

Lesson: Load Functions: +page.ts vs +page.server.ts

18. In SvelteKit 2, how do you trigger a redirect from a server load?

  1. Call redirect(303, "/login") from @sveltejs/kit
  2. Return { redirect: "/login" }
  3. Set window.location in the load
  4. Throw new Error("redirect")
Show answer

Answer: Call redirect(303, "/login") from @sveltejs/kit

redirect throws a special object for you.

Lesson: Error Pages and Redirects

Forms, Endpoints and Hooks

19. What does use:enhance add to a SvelteKit form?

  1. It is required or the form cannot submit
  2. Server-side validation
  3. Automatic database writes
  4. Submission via fetch without a full page reload, while still working without JavaScript
Show answer

Answer: Submission via fetch without a full page reload, while still working without JavaScript

Progressive enhancement layers on top of plain HTML forms.

Lesson: Form Actions and use:enhance

20. Where is the usual place to attach the current user for all server code?

  1. A global variable in a .svelte.ts module
  2. event.locals inside the handle hook in hooks.server.ts
  3. The page component props
  4. localStorage
Show answer

Answer: event.locals inside the handle hook in hooks.server.ts

locals is per request and safe on the server.

Lesson: +server.ts Routes and Hooks

21. Which variable can be imported into code that runs in the browser?

  1. DATABASE_URL from $env/static/private
  2. PUBLIC_API_BASE from $env/static/public
  3. Any variable from $env/dynamic/private
  4. process.env.SECRET_KEY
Show answer

Answer: PUBLIC_API_BASE from $env/static/public

Only public-prefixed variables reach the client.

Lesson: Environment Variables With $env

Shipping to Production

22. Which page option produces static HTML for a route at build time?

  1. export const static = "yes"
  2. export const ssr = false
  3. export const csr = true
  4. export const prerender = true
Show answer

Answer: export const prerender = true

Prerendering happens during the build.

Lesson: Rendering Modes and Adapters

23. Which tool is best suited to verify a form action redirect end to end?

  1. Playwright against the running app
  2. A Vitest unit test of a utility function
  3. svelte-check
  4. The Svelte compiler warnings
Show answer

Answer: Playwright against the running app

Routing and server behaviour need a real browser and server.

Lesson: Testing With Vitest and Playwright

24. How do you let slow, non-critical data arrive after the page first renders?

  1. Set export const ssr = true
  2. Move the query into an $effect on the server
  3. Return an un-awaited promise from the server load and use {#await} in the page
  4. Wrap the page in a {#key} block
Show answer

Answer: Return an un-awaited promise from the server load and use {#await} in the page

SvelteKit streams promises returned from load.

Lesson: Performance Techniques

25. Which item belongs on a SvelteKit production checklist?

  1. Import $env/static/private in client components
  2. Store per-user data in module-level $state on the server
  3. Form actions validate input on the server even if the browser validates too
  4. Use $effect to compute every derived value
Show answer

Answer: Form actions validate input on the server even if the browser validates too

Server validation and safe state handling come first.

Lesson: A Svelte and SvelteKit Checklist

← Back to the course