Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 35 ยท Practice and Review

Svelte and SvelteKit Interview Questions

Prepare for Svelte interviews with clear answers on runes, reactivity, compilation, components, snippets, SvelteKit data loading, SSR, performance and common pitfalls.

Interviewers rarely want a memorized definition. They want to hear that you understand why Svelte works the way it does and can reason about trade-offs. This lesson collects the questions that come up most often, grouped by topic, with answers you can say in your own words and small experiments to back them up. It is also a good last review of the whole track.

Svelte code on this page is read-only (the compiler is not available here). The <TryIt> blocks are plain-JavaScript stand-ins that demonstrate the ideas behind the answers.

Fundamentals

What is Svelte, and how is it different from React or Vue? Svelte is a component framework that does its work in a compiler. At build time it turns .svelte files into small, direct DOM-updating JavaScript. There is no virtual DOM diffing at runtime: when state changes, the generated code updates exactly the text nodes and attributes that depend on it. Result: small bundles, little runtime overhead and templates that look like HTML.

What is a .svelte file made of? Up to three parts: a <script> (logic, props, state), markup (HTML with , {#if}, {#each}), and an optional <style> whose CSS is scoped to the component by default (the compiler adds a unique class).

What changed in Svelte 5? Reactivity moved from compiler magic on let and $: to explicit runes: $state, $derived, $effect, $props, $bindable. Slots became snippets ({#snippet} and {@render}), on:click became the onclick attribute, createEventDispatcher was replaced by callback props, and runes work in .svelte.js files so reactive logic is no longer tied to components.

Reactivity and runes

Explain $state, $derived and $effect and when to use each. $state holds mutable reactive data; objects and arrays become deep proxies so todos.push(x) just works. $derived computes a value from other reactive values, cached and recomputed only when its inputs change. $effect runs side effects (DOM APIs, timers, network, logging) after the DOM updates and re-runs when values it read change. Rule of thumb: compute with $derived, react to the outside world with $effect, never use $effect to copy one state into another.

Why does destructuring break reactivity? Reading let { a } = state copies the current value once. Later changes to state.a do not update a. Read the property where you need it, or wrap it in $derived. (Props are the exception: let { a } = $props() stays reactive because the compiler handles it.)

What is the difference between $state and $state.raw? $state deeply proxies objects so mutations are tracked. $state.raw does not: only reassigning the variable triggers updates. Use raw for large immutable data.

This stand-in shows the core idea shared by signals-based frameworks: reading a value inside an effect subscribes the effect, writing re-runs it.

JS Signals in 25 lines: state, derived and effect

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real Svelte adds batching (several writes, one update), laziness for deriveds, cleanup functions and deep proxies, but the subscribe-on-read, notify-on-write model is the same.

Why can an effect loop forever, and how do you avoid it? An effect that writes to state it also reads re-triggers itself. Avoid it by deriving instead of syncing, by reading with untrack(() => x) when you do not want a dependency, or by restructuring so the write happens in an event handler.

What does bind: do, and what is $bindable? bind:value={x} creates two-way flow between an element or component prop and a variable. A child component must opt in with let { value = $bindable() } = $props(); otherwise props are one-way (parent to child).

Templates and components

Why do keyed each blocks matter? {#each items as item (item.id)} lets Svelte identify items across updates: it moves, inserts or removes DOM nodes instead of rewriting them in place. Without a key, items are matched by position, so item state (focus, input text, animation) can attach to the wrong row.

What are snippets, and how do they replace slots? A snippet is a reusable block of markup declared with {#snippet name(params)} and displayed with {@render name(args)}. They can take parameters, be passed as props (children is the default one) and be typed. They are more powerful than slots because they are first-class values.

How do components communicate? Props down, callback props up, bind: for deliberate two-way flow, context (setContext/getContext) for subtree-wide values, and shared state in .svelte.js modules for global state. Avoid reaching for global state when a prop will do.

How is CSS scoped? The compiler adds a hash class to each element in the component and rewrites selectors to include it. Use :global(...) to escape scoping and CSS custom properties to theme child components from outside.

Motion

When do you use transition: versus animate:? transition: animates elements entering or leaving a block; animate: (usually flip) animates elements that stay but change position in a keyed each block. crossfade links a leaving element to an entering one across lists. See Transitions and Flip and Crossfade.

Tween versus Spring? Tween runs to a target over a fixed duration with easing; Spring simulates physics (stiffness, damping) and handles rapid retargeting naturally. See Tween and Spring.

SvelteKit

What is SvelteKit and what does it add to Svelte? The application framework: file-based routing, layouts, server-side rendering with hydration, load functions, form actions, API endpoints, hooks, prerendering and adapters to deploy anywhere.

Universal versus server load? +page.js runs on the server for the first request and in the browser afterwards; use it for public APIs and non-serializable return values. +page.server.js runs only on the server and can use secrets, databases and cookies; its return value must be serializable.

What are form actions and why are they good? Server functions exported as actions and invoked by standard HTML form posts. They work without JavaScript; use:enhance upgrades them to in-place updates. Validation errors come back with fail(), success redirects with redirect(). See Form Actions.

SSR, SSG, SPA: how do you choose in SvelteKit? Per route with page options: prerender = true for static content, ssr = false for client-only pages, defaults for server-rendered pages, and csr = false for zero-JS pages. Pick an adapter to match the host. See Rendering Modes and Deployment.

How do you protect routes? Authenticate in hooks.server.js handle (read the session cookie, set event.locals.user), then check locals.user in server load functions and actions. A client-side check alone is not security because users control the browser.

This sketch of "guard in the load function" can be run:

JS Guarding a route in load

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quality and performance

How do you test a Svelte app? Vitest for unit tests, @testing-library/svelte for components (query by role, simulate user events), .svelte.test.js for runes with flushSync, Playwright for end-to-end. See Testing Svelte.

How do you make a Svelte app faster? Prefer $derived over effects, key lists, use $state.raw for big data, lazy-load heavy components, prerender, ship less JavaScript and measure first. See Debugging and Performance.

How do you debug reactivity? $inspect and $inspect.trace(), {@debug}, $state.snapshot for plain copies, Svelte DevTools and svelte-check.

What accessibility help does Svelte give? Compile-time a11y_* warnings for missing alt text, unlabeled controls and click handlers on non-interactive elements. Fix them with semantic HTML. See Accessibility.

Coding questions you may be asked

  1. Build a counter, then a todo list, then add filtering (see Practice Exercises).
  2. Implement a debounced search input that cancels stale requests (Live Search).
  3. Explain why this is buggy and fix it:
HTML
<script>
  let user = $state({ name: 'Ada' });
  let { name } = user;          // copies once
</script>
<p>{name}</p>                   <!-- stays "Ada" even after user.name changes -->

The fix is const name = $derived(user.name) or reading user.name directly in the markup.

  1. Why does this effect loop, and how would you fix it?
JavaScript
let count = $state(0);
$effect(() => { count = count + 1; });   // reads and writes count

The fix is to move the increment into an event handler, or derive the value instead.

Tips for the interview

  • Answer in layers: one-sentence definition, why it exists, one small example, one pitfall.
  • Compare honestly: Svelte's strengths are small output and simple code; the ecosystem is smaller than React's.
  • Use accurate terms: runes, snippets, hydration, prerender, adapter.
  • If you are unsure, reason from first principles (compile time versus run time, where the code runs) instead of guessing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main architectural difference of Svelte compared to a virtual-DOM framework?

    The compiler generates code that updates exactly what depends on changed state; there is no virtual DOM diff at runtime.
  2. 2. When should you reach for $effect?

    $derived is for computed values; effects are for side effects that cannot be expressed as derived state.
  3. 3. Where must code that uses a database password run in SvelteKit?

    Server-only modules never reach the browser; SvelteKit even fails the build if client code imports them.
  4. 4. Why is a client-side route check not enough to protect data?

    Anything running in the browser can be bypassed. Check permissions in server load functions, actions and endpoints.

Summary

  • Know the story: a compiler-based framework, runes for explicit reactivity, snippets instead of slots, callback props instead of dispatchers.
  • Use $derived to compute, $effect for side effects, keyed each blocks for lists and bind: deliberately.
  • For SvelteKit, explain load (universal vs server), form actions, hooks, page options and adapters, and always authorize on the server.
  • Show quality habits: tests, accessibility, debugging tools and measured performance fixes.

That completes the track. Head back to the Svelte tutorial index to revisit any chapter, or build your own project from the ideas in the Practice chapter.

Next: Back to the Svelte course