Skip to main content

Reactive $: statements

In runes mode, reactions to state updates are handled with the $derived and $effect runes.

In legacy mode, any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it with a $: label. These statements run after other code in the <script> and before the component markup is rendered, then whenever the values that they depend on change.

<script>
	let a = 1;
	let b = 2;

	// this is a 'reactive statement', and it will re-run
	// when `a`, `b` or `sum` change
	$: console.log(`${a} + ${b} = ${sum}`);

	// this is a 'reactive assignment' — `sum` will be
	// recalculated when `a` or `b` change. It is
	// not necessary to declare `sum` separately
	$: sum = a + b;
</script>

Statements are ordered topologically by their dependencies and their assignments: since the console.log statement depends on sum, sum is calculated first even though it appears later in the source.

Multiple statements can be combined by putting them in a block:

$: {
	// recalculate `total` when `items` changes
	total = 0;

	for (const const item: anyitem of items) {
		total += const item: anyitem.value;
	}
}

The left-hand side of a reactive assignments can be an identifier, or it can be a destructuring assignment:

$: ({ larry: anylarry, moe: anymoe, curly: anycurly } = stooges);

Understanding dependencies

The dependencies of a $: statement are determined at compile time — they are whichever variables are referenced (but not assigned to) inside the statement.

In other words, a statement like this will not re-run when count changes, because the compiler cannot 'see' the dependency:

let let count: numbercount = 0;
let let double: () => numberdouble = () => let count: numbercount * 2;

$: doubled = let double: () => numberdouble();

Similarly, topological ordering will fail if dependencies are referenced indirectly: z will never update, because y is not considered 'dirty' when the update occurs. Moving $: z = y below $: setY(x) will fix it:

<script>
	let x = 0;
	let y = 0;

	$: z = y;
	$: setY(x);

	function setY(value) {
		y = value;
	}
</script>

Browser-only code

Reactive statements run during server-side rendering as well as in the browser. This means that any code that should only run in the browser must be wrapped in an if block:

$: if (browser) {
	var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.Document.title: string

The document.title property gets or sets the current title of the document. When present, it defaults to the value of the .</p> <p><a href="https://developer.mozilla.org/docs/Web/API/Document/title">MDN Reference</a></p> </div></span>title</span></span> <span style="color:var(--shiki-foreground)"></span><span style="color:var(--shiki-token-keyword)">=</span> <span style="color:var(--shiki-foreground)"></span><span style="color:var(--shiki-foreground)">title</span><span style="color:var(--shiki-foreground)">;</span></span> <span class="line"><span style="color:var(--shiki-foreground)">}</span></span></code></pre></div><!----><!----></div><!----></div> <p class="edit svelte-s202gb"><a href="https://github.com/sveltejs/svelte/edit/main/documentation/docs/99-legacy/02-legacy-reactive-assignments.md" class="svelte-s202gb"><svg width="20" height="20" class="svelte-hfc14b"><use href="#edit"></use></svg><!----> Edit this page on GitHub</a> <!--[0--><a href="/docs/svelte/legacy-reactive-assignments/llms.txt" class="svelte-s202gb"><svg width="20" height="20" class="svelte-hfc14b"><use href="#contents"></use></svg><!----> llms.txt</a><!--]--></p> <div class="controls svelte-s202gb"><div class="flex svelte-s202gb"><span class="svelte-s202gb">previous</span> <span class="next svelte-s202gb">next</span></div> <div class="flex svelte-s202gb"><!--[0--><a href="/docs/svelte/legacy-let" class="svelte-s202gb">Reactive let/var declarations</a><!--]--> <!--[0--><a class="next svelte-s202gb" href="/docs/svelte/legacy-export-let">export let</a><!--]--></div></div><!----></div><!--]--><!----></div></div><!--]--><!--]--><!----><!----></main> <!--[0--><!--[-1--><!--]--><!--]--><!----><!----> <!--[-1--><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> <script> { __sveltekit_cp6wt6 = { base: new URL("../..", location).pathname.slice(0, -1) }; const element = document.currentScript.parentElement; Promise.all([ import("../../_app/immutable/entry/start.CFZCCMBG.js"), import("../../_app/immutable/entry/app.B29XSrNQ.js") ]).then(([kit, app]) => { kit.start(app, element, { node_ids: [0, 3, 16], data: [{type:"data",data:{nav_links:[{title:"Docs",slug:"docs",sections:[{title:"Svelte",path:"/docs/svelte",sections:[{title:"Introduction",sections:[{title:"Overview",path:"/docs/svelte/overview"},{title:"Getting started",path:"/docs/svelte/getting-started"},{title:".svelte files",path:"/docs/svelte/svelte-files"},{title:".svelte.js and .svelte.ts files",path:"/docs/svelte/svelte-js-files"}]},{title:"Runes",sections:[{title:"What are runes?",path:"/docs/svelte/what-are-runes"},{title:"$state",path:"/docs/svelte/$state"},{title:"$derived",path:"/docs/svelte/$derived"},{title:"$effect",path:"/docs/svelte/$effect"},{title:"$props",path:"/docs/svelte/$props"},{title:"$bindable",path:"/docs/svelte/$bindable"},{title:"$inspect",path:"/docs/svelte/$inspect"},{title:"$host",path:"/docs/svelte/$host"}]},{title:"Template syntax",sections:[{title:"Basic markup",path:"/docs/svelte/basic-markup"},{title:"{#if ...}",path:"/docs/svelte/if"},{title:"{#each ...}",path:"/docs/svelte/each"},{title:"{#key ...}",path:"/docs/svelte/key"},{title:"{#await ...}",path:"/docs/svelte/await"},{title:"{#snippet ...}",path:"/docs/svelte/snippet"},{title:"{@render ...}",path:"/docs/svelte/@render"},{title:"{@html ...}",path:"/docs/svelte/@html"},{title:"{@attach ...}",path:"/docs/svelte/@attach"},{title:"{@const ...}",path:"/docs/svelte/@const"},{title:"{@debug ...}",path:"/docs/svelte/@debug"},{title:"{let/const ...}",path:"/docs/svelte/declaration-tags"},{title:"bind:",path:"/docs/svelte/bind"},{title:"use:",path:"/docs/svelte/use"},{title:"transition:",path:"/docs/svelte/transition"},{title:"in: and out:",path:"/docs/svelte/in-and-out"},{title:"animate:",path:"/docs/svelte/animate"},{title:"style:",path:"/docs/svelte/style"},{title:"class",path:"/docs/svelte/class"},{title:"await",path:"/docs/svelte/await-expressions"}]},{title:"Styling",sections:[{title:"Scoped styles",path:"/docs/svelte/scoped-styles"},{title:"Global styles",path:"/docs/svelte/global-styles"},{title:"Custom properties",path:"/docs/svelte/custom-properties"},{title:"Nested \u003Cstyle> elements",path:"/docs/svelte/nested-style-elements"}]},{title:"Special elements",sections:[{title:"\u003Csvelte:boundary>",path:"/docs/svelte/svelte-boundary"},{title:"\u003Csvelte:window>",path:"/docs/svelte/svelte-window"},{title:"\u003Csvelte:document>",path:"/docs/svelte/svelte-document"},{title:"\u003Csvelte:body>",path:"/docs/svelte/svelte-body"},{title:"\u003Csvelte:head>",path:"/docs/svelte/svelte-head"},{title:"\u003Csvelte:element>",path:"/docs/svelte/svelte-element"},{title:"\u003Csvelte:options>",path:"/docs/svelte/svelte-options"}]},{title:"Runtime",sections:[{title:"Stores",path:"/docs/svelte/stores"},{title:"Context",path:"/docs/svelte/context"},{title:"Lifecycle hooks",path:"/docs/svelte/lifecycle-hooks"},{title:"Imperative component API",path:"/docs/svelte/imperative-component-api"},{title:"Hydratable data",path:"/docs/svelte/hydratable"}]},{title:"Misc",sections:[{title:"Best practices",path:"/docs/svelte/best-practices"},{title:"Testing",path:"/docs/svelte/testing"},{title:"TypeScript",path:"/docs/svelte/typescript"},{title:"Custom elements",path:"/docs/svelte/custom-elements"},{title:"Browser support",path:"/docs/svelte/browser-support"},{title:"Svelte 4 migration guide",path:"/docs/svelte/v4-migration-guide"},{title:"Svelte 5 migration guide",path:"/docs/svelte/v5-migration-guide"},{title:"Frequently asked questions",path:"/docs/svelte/faq"}]},{title:"Reference",sections:[{title:"svelte",path:"/docs/svelte/svelte"},{title:"svelte/action",path:"/docs/svelte/svelte-action"},{title:"svelte/animate",path:"/docs/svelte/svelte-animate"},{title:"svelte/attachments",path:"/docs/svelte/svelte-attachments"},{title:"svelte/compiler",path:"/docs/svelte/svelte-compiler"},{title:"svelte/easing",path:"/docs/svelte/svelte-easing"},{title:"svelte/events",path:"/docs/svelte/svelte-events"},{title:"svelte/legacy",path:"/docs/svelte/svelte-legacy"},{title:"svelte/motion",path:"/docs/svelte/svelte-motion"},{title:"svelte/reactivity/window",path:"/docs/svelte/svelte-reactivity-window"},{title:"svelte/reactivity",path:"/docs/svelte/svelte-reactivity"},{title:"svelte/server",path:"/docs/svelte/svelte-server"},{title:"svelte/store",path:"/docs/svelte/svelte-store"},{title:"svelte/transition",path:"/docs/svelte/svelte-transition"},{title:"Compiler errors",path:"/docs/svelte/compiler-errors"},{title:"Compiler warnings",path:"/docs/svelte/compiler-warnings"},{title:"Runtime errors",path:"/docs/svelte/runtime-errors"},{title:"Runtime warnings",path:"/docs/svelte/runtime-warnings"}]},{title:"Legacy APIs",sections:[{title:"Overview",path:"/docs/svelte/legacy-overview"},{title:"Reactive let/var declarations",path:"/docs/svelte/legacy-let"},{title:"Reactive $: statements",path:"/docs/svelte/legacy-reactive-assignments"},{title:"export let",path:"/docs/svelte/legacy-export-let"},{title:"$$props and $$restProps",path:"/docs/svelte/legacy-$$props-and-$$restProps"},{title:"on:",path:"/docs/svelte/legacy-on"},{title:"\u003Cslot>",path:"/docs/svelte/legacy-slots"},{title:"$$slots",path:"/docs/svelte/legacy-$$slots"},{title:"\u003Csvelte:fragment>",path:"/docs/svelte/legacy-svelte-fragment"},{title:"\u003Csvelte:component>",path:"/docs/svelte/legacy-svelte-component"},{title:"\u003Csvelte:self>",path:"/docs/svelte/legacy-svelte-self"},{title:"Imperative component API",path:"/docs/svelte/legacy-component-api"}]}]},{title:"SvelteKit",path:"/docs/kit",sections:[{title:"Getting started",sections:[{title:"Introduction",path:"/docs/kit/introduction"},{title:"Creating a project",path:"/docs/kit/creating-a-project"},{title:"Project types",path:"/docs/kit/project-types"},{title:"Project structure",path:"/docs/kit/project-structure"},{title:"Web standards",path:"/docs/kit/web-standards"}]},{title:"Core concepts",sections:[{title:"Routing",path:"/docs/kit/routing"},{title:"Loading data",path:"/docs/kit/load"},{title:"Form actions",path:"/docs/kit/form-actions"},{title:"Page options",path:"/docs/kit/page-options"},{title:"State management",path:"/docs/kit/state-management"},{title:"Remote functions",path:"/docs/kit/remote-functions"},{title:"Environment variables",path:"/docs/kit/environment-variables"}]},{title:"Build and deploy",sections:[{title:"Building your app",path:"/docs/kit/building-your-app"},{title:"Adapters",path:"/docs/kit/adapters"},{title:"Zero-config deployments",path:"/docs/kit/adapter-auto"},{title:"Node servers",path:"/docs/kit/adapter-node"},{title:"Static site generation",path:"/docs/kit/adapter-static"},{title:"Single-page apps",path:"/docs/kit/single-page-apps"},{title:"Cloudflare",path:"/docs/kit/adapter-cloudflare"},{title:"Cloudflare Workers",path:"/docs/kit/adapter-cloudflare-workers"},{title:"Netlify",path:"/docs/kit/adapter-netlify"},{title:"Vercel",path:"/docs/kit/adapter-vercel"},{title:"Writing adapters",path:"/docs/kit/writing-adapters"}]},{title:"Advanced",sections:[{title:"Advanced routing",path:"/docs/kit/advanced-routing"},{title:"Hooks",path:"/docs/kit/hooks"},{title:"Errors",path:"/docs/kit/errors"},{title:"Link options",path:"/docs/kit/link-options"},{title:"Service workers",path:"/docs/kit/service-workers"},{title:"Server-only modules",path:"/docs/kit/server-only-modules"},{title:"Snapshots",path:"/docs/kit/snapshots"},{title:"Shallow routing",path:"/docs/kit/shallow-routing"},{title:"Observability",path:"/docs/kit/observability"},{title:"Packaging",path:"/docs/kit/packaging"}]},{title:"Best practices",sections:[{title:"Auth",path:"/docs/kit/auth"},{title:"Performance",path:"/docs/kit/performance"},{title:"Icons",path:"/docs/kit/icons"},{title:"Images",path:"/docs/kit/images"},{title:"Accessibility",path:"/docs/kit/accessibility"},{title:"SEO",path:"/docs/kit/seo"}]},{title:"Appendix",sections:[{title:"Frequently asked questions",path:"/docs/kit/faq"},{title:"Integrations",path:"/docs/kit/integrations"},{title:"Breakpoint Debugging",path:"/docs/kit/debugging"},{title:"Migrating to SvelteKit v2",path:"/docs/kit/migrating-to-sveltekit-2"},{title:"Migrating from Sapper",path:"/docs/kit/migrating"},{title:"Additional resources",path:"/docs/kit/additional-resources"},{title:"Glossary",path:"/docs/kit/glossary"}]},{title:"Reference",sections:[{title:"@sveltejs/kit",path:"/docs/kit/@sveltejs-kit"},{title:"@sveltejs/kit/hooks",path:"/docs/kit/@sveltejs-kit-hooks"},{title:"@sveltejs/kit/node",path:"/docs/kit/@sveltejs-kit-node"},{title:"@sveltejs/kit/vite",path:"/docs/kit/@sveltejs-kit-vite"},{title:"$app/env",path:"/docs/kit/$app-env"},{title:"$app/env/private",path:"/docs/kit/$app-env-private"},{title:"$app/env/public",path:"/docs/kit/$app-env-public"},{title:"$app/environment",path:"/docs/kit/$app-environment"},{title:"$app/forms",path:"/docs/kit/$app-forms"},{title:"$app/navigation",path:"/docs/kit/$app-navigation"},{title:"$app/paths",path:"/docs/kit/$app-paths"},{title:"$app/server",path:"/docs/kit/$app-server"},{title:"$app/state",path:"/docs/kit/$app-state"},{title:"$app/stores",path:"/docs/kit/$app-stores"},{title:"$app/types",path:"/docs/kit/$app-types"},{title:"$env/dynamic/private",path:"/docs/kit/$env-dynamic-private"},{title:"$env/dynamic/public",path:"/docs/kit/$env-dynamic-public"},{title:"$env/static/private",path:"/docs/kit/$env-static-private"},{title:"$env/static/public",path:"/docs/kit/$env-static-public"},{title:"$lib",path:"/docs/kit/$lib"},{title:"$service-worker",path:"/docs/kit/$service-worker"},{title:"Configuration",path:"/docs/kit/configuration"},{title:"Command Line Interface",path:"/docs/kit/cli"},{title:"Types",path:"/docs/kit/types"}]}]},{title:"CLI",path:"/docs/cli",sections:[{title:"Introduction",sections:[{title:"Overview",path:"/docs/cli/overview"},{title:"Frequently asked questions",path:"/docs/cli/faq"}]},{title:"Commands",sections:[{title:"sv create",path:"/docs/cli/sv-create"},{title:"sv add",path:"/docs/cli/sv-add"},{title:"sv check",path:"/docs/cli/sv-check"},{title:"sv migrate",path:"/docs/cli/sv-migrate"}]},{title:"Add-ons",sections:[{title:"better-auth",path:"/docs/cli/better-auth"},{title:"drizzle",path:"/docs/cli/drizzle"},{title:"eslint",path:"/docs/cli/eslint"},{title:"experimental",path:"/docs/cli/experimental"},{title:"mcp",path:"/docs/cli/mcp"},{title:"mdsvex",path:"/docs/cli/mdsvex"},{title:"paraglide",path:"/docs/cli/paraglide"},{title:"playwright",path:"/docs/cli/playwright"},{title:"prettier",path:"/docs/cli/prettier"},{title:"storybook",path:"/docs/cli/storybook"},{title:"sveltekit-adapter",path:"/docs/cli/sveltekit-adapter"},{title:"tailwindcss",path:"/docs/cli/tailwind"},{title:"vitest",path:"/docs/cli/vitest"},{title:"[create your own]",path:"/docs/cli/community"}]},{title:"API",sections:[{title:"sv",path:"/docs/cli/sv"},{title:"sv-utils",path:"/docs/cli/sv-utils"}]}]},{title:"AI",path:"/docs/ai",sections:[{title:"Introduction",sections:[{title:"Overview",path:"/docs/ai/overview"}]},{title:"Instructions",sections:[{title:"AGENTS.md",path:"/docs/ai/instructions"}]},{title:"MCP server",sections:[{title:"Overview",path:"/docs/ai/mcp"},{title:"Local setup",path:"/docs/ai/local-setup"},{title:"Remote setup",path:"/docs/ai/remote-setup"},{title:"Tools",path:"/docs/ai/tools"},{title:"Resources",path:"/docs/ai/resources"},{title:"Prompts",path:"/docs/ai/prompts"},{title:"CLI",path:"/docs/ai/cli"}]},{title:"Skills",sections:[{title:"Overview",path:"/docs/ai/skills"}]},{title:"Subagents",sections:[{title:"Overview",path:"/docs/ai/subagent"}]},{title:"Plugins",sections:[{title:"Claude Code",path:"/docs/ai/claude-plugin"},{title:"OpenCode",path:"/docs/ai/opencode-plugin"},{title:"Cursor",path:"/docs/ai/cursor-plugin"},{title:"GitHub Copilot CLI",path:"/docs/ai/copilot-plugin"},{title:"Codex CLI",path:"/docs/ai/codex-plugin"}]}]}]},{title:"Tutorial",slug:"tutorial",sections:[{title:"Basic Svelte",sections:[{title:"Introduction",sections:[{title:"Welcome to Svelte",path:"/tutorial/svelte/welcome-to-svelte"},{title:"Your first component",path:"/tutorial/svelte/your-first-component"},{title:"Dynamic attributes",path:"/tutorial/svelte/dynamic-attributes"},{title:"Styling",path:"/tutorial/svelte/styling"},{title:"Nested components",path:"/tutorial/svelte/nested-components"},{title:"HTML tags",path:"/tutorial/svelte/html-tags"}]},{title:"Reactivity",sections:[{title:"State",path:"/tutorial/svelte/state"},{title:"Deep state",path:"/tutorial/svelte/deep-state"},{title:"Derived state",path:"/tutorial/svelte/derived-state"},{title:"Inspecting state",path:"/tutorial/svelte/inspecting-state"},{title:"Effects",path:"/tutorial/svelte/effects"},{title:"Universal reactivity",path:"/tutorial/svelte/universal-reactivity"}]},{title:"Props",sections:[{title:"Declaring props",path:"/tutorial/svelte/declaring-props"},{title:"Default values",path:"/tutorial/svelte/default-values"},{title:"Spread props",path:"/tutorial/svelte/spread-props"}]},{title:"Logic",sections:[{title:"If blocks",path:"/tutorial/svelte/if-blocks"},{title:"Else blocks",path:"/tutorial/svelte/else-blocks"},{title:"Else-if blocks",path:"/tutorial/svelte/else-if-blocks"},{title:"Each blocks",path:"/tutorial/svelte/each-blocks"},{title:"Keyed each blocks",path:"/tutorial/svelte/keyed-each-blocks"},{title:"Await blocks",path:"/tutorial/svelte/await-blocks"}]},{title:"Events",sections:[{title:"DOM events",path:"/tutorial/svelte/dom-events"},{title:"Inline handlers",path:"/tutorial/svelte/inline-handlers"},{title:"Capturing",path:"/tutorial/svelte/capturing"},{title:"Component events",path:"/tutorial/svelte/component-events"},{title:"Spreading events",path:"/tutorial/svelte/spreading-events"}]},{title:"Bindings",sections:[{title:"Text inputs",path:"/tutorial/svelte/text-inputs"},{title:"Numeric inputs",path:"/tutorial/svelte/numeric-inputs"},{title:"Checkbox inputs",path:"/tutorial/svelte/checkbox-inputs"},{title:"Select bindings",path:"/tutorial/svelte/select-bindings"},{title:"Group inputs",path:"/tutorial/svelte/group-inputs"},{title:"Select multiple",path:"/tutorial/svelte/multiple-select-bindings"},{title:"Textarea inputs",path:"/tutorial/svelte/textarea-inputs"}]},{title:"Classes and styles",sections:[{title:"The class attribute",path:"/tutorial/svelte/classes"},{title:"The style directive",path:"/tutorial/svelte/styles"},{title:"Component styles",path:"/tutorial/svelte/component-styles"}]},{title:"Attachments",sections:[{title:"The attach tag",path:"/tutorial/svelte/attach"},{title:"Attachment factories",path:"/tutorial/svelte/attachment-factories"}]},{title:"Transitions",sections:[{title:"The transition directive",path:"/tutorial/svelte/transition"},{title:"Adding parameters",path:"/tutorial/svelte/adding-parameters-to-transitions"},{title:"In and out",path:"/tutorial/svelte/in-and-out"},{title:"Custom CSS transitions",path:"/tutorial/svelte/custom-css-transitions"},{title:"Custom JS transitions",path:"/tutorial/svelte/custom-js-transitions"},{title:"Transition events",path:"/tutorial/svelte/transition-events"},{title:"Global transitions",path:"/tutorial/svelte/global-transitions"},{title:"Key blocks",path:"/tutorial/svelte/key-blocks"}]}]},{title:"Advanced Svelte",sections:[{title:"Advanced reactivity",sections:[{title:"Raw state",path:"/tutorial/svelte/raw-state"},{title:"Reactive classes",path:"/tutorial/svelte/reactive-classes"},{title:"Getters and setters",path:"/tutorial/svelte/getters-and-setters"},{title:"Reactive built-ins",path:"/tutorial/svelte/reactive-builtins"},{title:"Stores",path:"/tutorial/svelte/stores"}]},{title:"Reusing content",sections:[{title:"Snippets and render tags",path:"/tutorial/svelte/snippets-and-render-tags"},{title:"Passing snippets to components",path:"/tutorial/svelte/passing-snippets"},{title:"Implicit snippet props",path:"/tutorial/svelte/implicit-snippet-props"}]},{title:"Motion",sections:[{title:"Tweened values",path:"/tutorial/svelte/tweens"},{title:"Springs",path:"/tutorial/svelte/springs"}]},{title:"Advanced bindings",sections:[{title:"Contenteditable bindings",path:"/tutorial/svelte/contenteditable-bindings"},{title:"Each block bindings",path:"/tutorial/svelte/each-block-bindings"},{title:"Media elements",path:"/tutorial/svelte/media-elements"},{title:"Dimensions",path:"/tutorial/svelte/dimensions"},{title:"This",path:"/tutorial/svelte/bind-this"},{title:"Component bindings",path:"/tutorial/svelte/component-bindings"},{title:"Binding to component instances",path:"/tutorial/svelte/component-this"}]},{title:"Advanced transitions",sections:[{title:"Deferred transitions",path:"/tutorial/svelte/deferred-transitions"},{title:"Animations",path:"/tutorial/svelte/animations"}]},{title:"Context API",sections:[{title:"setContext and getContext",path:"/tutorial/svelte/context-api"}]},{title:"Special elements",sections:[{title:"\u003Csvelte:window>",path:"/tutorial/svelte/svelte-window"},{title:"\u003Csvelte:window> bindings",path:"/tutorial/svelte/svelte-window-bindings"},{title:"\u003Csvelte:document>",path:"/tutorial/svelte/svelte-document"},{title:"\u003Csvelte:body>",path:"/tutorial/svelte/svelte-body"},{title:"\u003Csvelte:head>",path:"/tutorial/svelte/svelte-head"},{title:"\u003Csvelte:element>",path:"/tutorial/svelte/svelte-element"},{title:"\u003Csvelte:boundary>",path:"/tutorial/svelte/svelte-boundary"}]},{title:"\u003Cscript module>",sections:[{title:"Sharing code",path:"/tutorial/svelte/sharing-code"},{title:"Exports",path:"/tutorial/svelte/module-exports"}]},{title:"Next steps",sections:[{title:"Congratulations!",path:"/tutorial/svelte/congratulations"}]}]},{title:"Basic SvelteKit",sections:[{title:"Introduction",sections:[{title:"What is SvelteKit?",path:"/tutorial/kit/introducing-sveltekit"}]},{title:"Routing",sections:[{title:"Pages",path:"/tutorial/kit/pages"},{title:"Layouts",path:"/tutorial/kit/layouts"},{title:"Route parameters",path:"/tutorial/kit/params"}]},{title:"Loading data",sections:[{title:"Page data",path:"/tutorial/kit/page-data"},{title:"Layout data",path:"/tutorial/kit/layout-data"}]},{title:"Headers and cookies",sections:[{title:"Setting headers",path:"/tutorial/kit/headers"},{title:"Reading and writing cookies",path:"/tutorial/kit/cookies"}]},{title:"Shared modules",sections:[{title:"The $lib alias",path:"/tutorial/kit/lib"}]},{title:"Forms",sections:[{title:"The \u003Cform> element",path:"/tutorial/kit/the-form-element"},{title:"Named form actions",path:"/tutorial/kit/named-form-actions"},{title:"Validation",path:"/tutorial/kit/form-validation"},{title:"Progressive enhancement",path:"/tutorial/kit/progressive-enhancement"},{title:"Customizing use:enhance",path:"/tutorial/kit/customizing-use-enhance"}]},{title:"API routes",sections:[{title:"GET handlers",path:"/tutorial/kit/get-handlers"},{title:"POST handlers",path:"/tutorial/kit/post-handlers"},{title:"Other handlers",path:"/tutorial/kit/other-handlers"}]},{title:"$app/state",sections:[{title:"page",path:"/tutorial/kit/page-state"},{title:"navigating",path:"/tutorial/kit/navigating-state"},{title:"updated",path:"/tutorial/kit/updated-state"}]},{title:"Errors and redirects",sections:[{title:"Basics",path:"/tutorial/kit/error-basics"},{title:"Error pages",path:"/tutorial/kit/error-pages"},{title:"Fallback errors",path:"/tutorial/kit/fallback-errors"},{title:"Redirects",path:"/tutorial/kit/redirects"}]}]},{title:"Advanced SvelteKit",sections:[{title:"Hooks",sections:[{title:"handle",path:"/tutorial/kit/handle"},{title:"The RequestEvent object",path:"/tutorial/kit/event"},{title:"handleFetch",path:"/tutorial/kit/handlefetch"},{title:"handleError",path:"/tutorial/kit/handleerror"}]},{title:"Page options",sections:[{title:"Basics",path:"/tutorial/kit/page-options"},{title:"ssr",path:"/tutorial/kit/ssr"},{title:"csr",path:"/tutorial/kit/csr"},{title:"prerender",path:"/tutorial/kit/prerender"},{title:"trailingSlash",path:"/tutorial/kit/trailingslash"}]},{title:"Link options",sections:[{title:"Preloading",path:"/tutorial/kit/preload"},{title:"Reloading the page",path:"/tutorial/kit/reload"}]},{title:"Advanced routing",sections:[{title:"Optional parameters",path:"/tutorial/kit/optional-params"},{title:"Rest parameters",path:"/tutorial/kit/rest-params"},{title:"Param matchers",path:"/tutorial/kit/param-matchers"},{title:"Route groups",path:"/tutorial/kit/route-groups"},{title:"Breaking out of layouts",path:"/tutorial/kit/breaking-out-of-layouts"}]},{title:"Advanced loading",sections:[{title:"Universal load functions",path:"/tutorial/kit/universal-load-functions"},{title:"Using both load functions",path:"/tutorial/kit/using-both-load-functions"},{title:"Using parent data",path:"/tutorial/kit/await-parent"},{title:"Invalidation",path:"/tutorial/kit/invalidation"},{title:"Custom dependencies",path:"/tutorial/kit/custom-dependencies"},{title:"invalidateAll",path:"/tutorial/kit/invalidate-all"}]},{title:"Environment variables",sections:[{title:"$env/static/private",path:"/tutorial/kit/env-static-private"},{title:"$env/dynamic/private",path:"/tutorial/kit/env-dynamic-private"},{title:"$env/static/public",path:"/tutorial/kit/env-static-public"},{title:"$env/dynamic/public",path:"/tutorial/kit/env-dynamic-public"}]},{title:"Conclusion",sections:[{title:"Next steps",path:"/tutorial/kit/next-steps"}]}]}]},{title:"Packages",slug:"packages"},{title:"Playground",slug:"playground"},{title:"Blog",slug:"blog"}],banner:{id:"barcelona-2025-tickets",start:new Date(1745193600000),end:new Date(1746575999000),arrow:true,content:{lg:"Svelte Summit Barcelona and online, May 8-9: Last few days to get tickets!",sm:"Svelte Summit May 8-9"},href:"https://www.sveltesummit.com/"}},uses:{}},{type:"data",data:{sections:[{slug:"docs/svelte/introduction",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Introduction"},children:[{slug:"docs/svelte/overview",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Overview"},children:[]},{slug:"docs/svelte/getting-started",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Getting started"},children:[]},{slug:"docs/svelte/svelte-files",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:".svelte files"},children:[]},{slug:"docs/svelte/svelte-js-files",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:".svelte.js and .svelte.ts files"},children:[]}]},{slug:"docs/svelte/runes",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Runes"},children:[{slug:"docs/svelte/what-are-runes",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"What are runes?"},children:[]},{slug:"docs/svelte/$state",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$state",tags:"rune-state"},children:[]},{slug:"docs/svelte/$derived",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$derived",tags:"rune-derived"},children:[]},{slug:"docs/svelte/$effect",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$effect",tags:"rune-effect"},children:[]},{slug:"docs/svelte/$props",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$props",tags:"rune-props"},children:[]},{slug:"docs/svelte/$bindable",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$bindable"},children:[]},{slug:"docs/svelte/$inspect",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$inspect",tags:"rune-inspect"},children:[]},{slug:"docs/svelte/$host",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$host"},children:[]}]},{slug:"docs/svelte/template-syntax",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Template syntax"},children:[{slug:"docs/svelte/basic-markup",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Basic markup"},children:[]},{slug:"docs/svelte/if",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{#if ...}",tags:"template-if"},children:[]},{slug:"docs/svelte/each",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{#each ...}",tags:"template-each"},children:[]},{slug:"docs/svelte/key",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{#key ...}",tags:"template-key"},children:[]},{slug:"docs/svelte/await",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{#await ...}",tags:"template-await"},children:[]},{slug:"docs/svelte/snippet",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{#snippet ...}"},children:[]},{slug:"docs/svelte/@render",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{@render ...}"},children:[]},{slug:"docs/svelte/@html",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{@html ...}",tags:"template-html"},children:[]},{slug:"docs/svelte/@attach",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{@attach ...}",tags:"attachments"},children:[]},{slug:"docs/svelte/@const",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{@const ...}"},children:[]},{slug:"docs/svelte/@debug",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{@debug ...}"},children:[]},{slug:"docs/svelte/declaration-tags",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"{let/const ...}"},children:[]},{slug:"docs/svelte/bind",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"bind:"},children:[]},{slug:"docs/svelte/use",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"use:"},children:[]},{slug:"docs/svelte/transition",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"transition:",tags:"transitions"},children:[]},{slug:"docs/svelte/in-and-out",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"in: and out:",tags:"transitions"},children:[]},{slug:"docs/svelte/animate",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"animate:"},children:[]},{slug:"docs/svelte/style",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"style:",tags:"template-style"},children:[]},{slug:"docs/svelte/class",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"class",tags:"template-style"},children:[]},{slug:"docs/svelte/await-expressions",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"await"},children:[]}]},{slug:"docs/svelte/styling",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Styling"},children:[{slug:"docs/svelte/scoped-styles",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Scoped styles",tags:"styles-scoped"},children:[]},{slug:"docs/svelte/global-styles",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Global styles",tags:"styles-global"},children:[]},{slug:"docs/svelte/custom-properties",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Custom properties",tags:"styles-custom-properties"},children:[]},{slug:"docs/svelte/nested-style-elements",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Nested \u003Cstyle> elements"},children:[]}]},{slug:"docs/svelte/special-elements",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Special elements"},children:[{slug:"docs/svelte/svelte-boundary",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:boundary>"},children:[]},{slug:"docs/svelte/svelte-window",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:window>"},children:[]},{slug:"docs/svelte/svelte-document",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:document>"},children:[]},{slug:"docs/svelte/svelte-body",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:body>"},children:[]},{slug:"docs/svelte/svelte-head",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:head>"},children:[]},{slug:"docs/svelte/svelte-element",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:element>"},children:[]},{slug:"docs/svelte/svelte-options",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:options>"},children:[]}]},{slug:"docs/svelte/runtime",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Runtime"},children:[{slug:"docs/svelte/stores",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Stores"},children:[]},{slug:"docs/svelte/context",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Context"},children:[]},{slug:"docs/svelte/lifecycle-hooks",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Lifecycle hooks"},children:[]},{slug:"docs/svelte/imperative-component-api",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Imperative component API"},children:[]},{slug:"docs/svelte/hydratable",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Hydratable data"},children:[]}]},{slug:"docs/svelte/misc",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Misc"},children:[{slug:"docs/svelte/best-practices",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Best practices",skill:true,name:"svelte-core-bestpractices",description:"Guidance on writing fast, robust, modern Svelte code. Load this skill whenever in a Svelte project and asked to write/edit or analyze a Svelte component or module. Covers reactivity, event handling, styling, integration with libraries and more."},children:[]},{slug:"docs/svelte/testing",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Testing"},children:[]},{slug:"docs/svelte/typescript",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"TypeScript"},children:[]},{slug:"docs/svelte/custom-elements",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Custom elements"},children:[]},{slug:"docs/svelte/browser-support",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Browser support"},children:[]},{slug:"docs/svelte/v4-migration-guide",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Svelte 4 migration guide"},children:[]},{slug:"docs/svelte/v5-migration-guide",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Svelte 5 migration guide"},children:[]},{slug:"docs/svelte/faq",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Frequently asked questions"},children:[]}]},{slug:"docs/svelte/reference",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Reference"},children:[{slug:"docs/svelte/svelte",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte"},children:[]},{slug:"docs/svelte/svelte-action",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/action"},children:[]},{slug:"docs/svelte/svelte-animate",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/animate"},children:[]},{slug:"docs/svelte/svelte-attachments",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/attachments",tags:"attachments"},children:[]},{slug:"docs/svelte/svelte-compiler",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/compiler"},children:[]},{slug:"docs/svelte/svelte-easing",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/easing"},children:[]},{slug:"docs/svelte/svelte-events",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/events"},children:[]},{slug:"docs/svelte/svelte-legacy",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/legacy"},children:[]},{slug:"docs/svelte/svelte-motion",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/motion"},children:[]},{slug:"docs/svelte/svelte-reactivity-window",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/reactivity/window"},children:[]},{slug:"docs/svelte/svelte-reactivity",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/reactivity"},children:[]},{slug:"docs/svelte/svelte-server",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/server"},children:[]},{slug:"docs/svelte/svelte-store",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/store"},children:[]},{slug:"docs/svelte/svelte-transition",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"svelte/transition",tags:"transitions"},children:[]},{slug:"docs/svelte/compiler-errors",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Compiler errors"},children:[]},{slug:"docs/svelte/compiler-warnings",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Compiler warnings"},children:[]},{slug:"docs/svelte/runtime-errors",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Runtime errors"},children:[]},{slug:"docs/svelte/runtime-warnings",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Runtime warnings"},children:[]}]},{slug:"docs/svelte/legacy",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Legacy APIs"},children:[{slug:"docs/svelte/legacy-overview",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Overview"},children:[]},{slug:"docs/svelte/legacy-let",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Reactive let/var declarations"},children:[]},{slug:"docs/svelte/legacy-reactive-assignments",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Reactive $: statements"},children:[]},{slug:"docs/svelte/legacy-export-let",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"export let"},children:[]},{slug:"docs/svelte/legacy-$$props-and-$$restProps",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$$props and $$restProps"},children:[]},{slug:"docs/svelte/legacy-on",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"on:"},children:[]},{slug:"docs/svelte/legacy-slots",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Cslot>"},children:[]},{slug:"docs/svelte/legacy-$$slots",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"$$slots"},children:[]},{slug:"docs/svelte/legacy-svelte-fragment",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:fragment>"},children:[]},{slug:"docs/svelte/legacy-svelte-component",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:component>"},children:[]},{slug:"docs/svelte/legacy-svelte-self",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"\u003Csvelte:self>"},children:[]},{slug:"docs/svelte/legacy-component-api",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Imperative component API"},children:[]}]}]},uses:{params:["topic"]}},{type:"data",data:{document:{slug:"docs/svelte/legacy-reactive-assignments",file:"docs/svelte/99-legacy/02-legacy-reactive-assignments.md",metadata:{NOTE:"do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts",title:"Reactive $: statements"},breadcrumbs:[{title:"Docs"},{title:"Svelte"},{title:"Legacy APIs"}],body:"\u003Cp>In runes mode, reactions to state updates are handled with the \u003Ca href=\"$derived\">\u003Ccode>$derived\u003C/code>\u003C/a> and \u003Ca href=\"$effect\">\u003Ccode>$effect\u003C/code>\u003C/a> runes.\u003C/p>\n\u003Cp>In legacy mode, any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it with a \u003Ccode>$:\u003C/code> \u003Ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label\">label\u003C/a>. These statements run after other code in the \u003Ccode><script>\u003C/code> and before the component markup is rendered, then whenever the values that they depend on change.\u003C/p>\n\u003Cdiv class=\"code-block\">\u003Cdiv class=\"controls\">\u003Cbutton class=\"copy-to-clipboard raised\" title=\"Copy to clipboard\" aria-label=\"Copy to clipboard\">\u003C/button>\u003C/div>\u003Cpre data-js data-ts data-language=\"svelte\" class=\"shiki css-variables\" style=\"background-color:var(--shiki-background);color:var(--shiki-foreground)\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\"><\u003C/span>\u003Cspan style=\"color:var(--shiki-token-string-expression)\">script\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">>\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">a\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">1\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">b\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">2\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-comment)\">// this is a 'reactive statement', and it will re-run\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-comment)\">// when `a`, `b` or `sum` change\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">console\u003C/span>\u003Cspan style=\"color:var(--shiki-token-function)\">.log\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">(\u003C/span>\u003Cspan style=\"color:var(--shiki-token-string-expression)\">`\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">${\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">a\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">}\u003C/span> \u003Cspan style=\"color:var(--shiki-token-string-expression)\">+\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">${\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">b\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">}\u003C/span> \u003Cspan style=\"color:var(--shiki-token-string-expression)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">${\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">sum\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">}\u003C/span>\u003Cspan style=\"color:var(--shiki-token-string-expression)\">`\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">);\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-comment)\">// this is a 'reactive assignment' — `sum` will be\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-comment)\">// recalculated when `a` or `b` change. It is\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-comment)\">// not necessary to declare `sum` separately\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">sum\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">a\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">+\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">b;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\"></\u003C/span>\u003Cspan style=\"color:var(--shiki-token-string-expression)\">script\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">>\u003C/span>\u003C/span>\u003C/code>\u003C/pre>\u003C/div>\u003Cp>Statements are ordered \u003Cem>topologically\u003C/em> by their dependencies and their assignments: since the \u003Ccode>console.log\u003C/code> statement depends on \u003Ccode>sum\u003C/code>, \u003Ccode>sum\u003C/code> is calculated first even though it appears later in the source.\u003C/p>\n\u003Cp>Multiple statements can be combined by putting them in a block:\u003C/p>\n\u003Cdiv class=\"code-block\">\u003Cdiv class=\"controls\">\u003Cbutton class=\"copy-to-clipboard raised\" title=\"Copy to clipboard\" aria-label=\"Copy to clipboard\">\u003C/button>\u003C/div>\u003Cpre data-js data-ts data-language=\"js\" class=\"shiki css-variables twoslash lsp\" style=\"background-color:var(--shiki-background);color:var(--shiki-foreground)\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">{\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-comment)\">// recalculate `total` when `items` changes\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">total\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">0\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-keyword)\">for\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">(\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">const\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-constant)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">const\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">item\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">any\u003C/span>\u003C/code>\u003C/span>item\u003C/span>\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">of\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">items\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">) {\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\t\u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">total\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">+=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-constant)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">const\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">item\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">any\u003C/span>\u003C/code>\u003C/span>item\u003C/span>\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">.\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">value\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">}\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\">}\u003C/span>\u003C/span>\u003C/code>\u003C/pre>\u003C/div>\u003Cp>The left-hand side of a reactive assignments can be an identifier, or it can be a destructuring assignment:\u003C/p>\n\u003Cdiv class=\"code-block\">\u003Cdiv class=\"controls\">\u003Cbutton class=\"copy-to-clipboard raised\" title=\"Copy to clipboard\" aria-label=\"Copy to clipboard\">\u003C/button>\u003C/div>\u003Cpre data-js data-ts data-language=\"js\" class=\"shiki css-variables twoslash lsp\" style=\"background-color:var(--shiki-background);color:var(--shiki-foreground)\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">({\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-foreground)\">larry\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">any\u003C/span>\u003C/code>\u003C/span>larry\u003C/span>\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">,\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-foreground)\">moe\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">any\u003C/span>\u003C/code>\u003C/span>moe\u003C/span>\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">,\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-foreground)\">curly\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">any\u003C/span>\u003C/code>\u003C/span>curly\u003C/span>\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">}\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">stooges\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">);\u003C/span>\u003C/span>\u003C/code>\u003C/pre>\u003C/div>\u003Ch2 id=\"Understanding-dependencies\">\u003Cspan>Understanding dependencies\u003C/span>\u003Ca href=\"#Understanding-dependencies\" class=\"permalink\" aria-label=\"permalink\">\u003C/a>\u003C/h2>\u003Cp>The dependencies of a \u003Ccode>$:\u003C/code> statement are determined at compile time — they are whichever variables are referenced (but not assigned to) inside the statement.\u003C/p>\n\u003Cp>In other words, a statement like this will \u003Cem>not\u003C/em> re-run when \u003Ccode>count\u003C/code> changes, because the compiler cannot 'see' the dependency:\u003C/p>\n\u003Cdiv class=\"code-block\">\u003Cdiv class=\"controls\">\u003Cbutton class=\"copy-to-clipboard raised\" title=\"Copy to clipboard\" aria-label=\"Copy to clipboard\">\u003C/button>\u003C/div>\u003Cpre data-js data-ts data-language=\"js\" class=\"shiki css-variables twoslash lsp\" style=\"background-color:var(--shiki-background);color:var(--shiki-foreground)\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">count\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">number\u003C/span>\u003C/code>\u003C/span>count\u003C/span>\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">0\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-function)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">double\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">()\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=>\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">number\u003C/span>\u003C/code>\u003C/span>double\u003C/span>\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">()\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=>\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">count\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">number\u003C/span>\u003C/code>\u003C/span>count\u003C/span>\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">*\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">2\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003C/span>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">doubled\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-function)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">double\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">()\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=>\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">number\u003C/span>\u003C/code>\u003C/span>double\u003C/span>\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">();\u003C/span>\u003C/span>\u003C/code>\u003C/pre>\u003C/div>\u003Cp>Similarly, topological ordering will fail if dependencies are referenced indirectly: \u003Ccode>z\u003C/code> will never update, because \u003Ccode>y\u003C/code> is not considered 'dirty' when the update occurs. Moving \u003Ccode>$: z = y\u003C/code> below \u003Ccode>$: setY(x)\u003C/code> will fix it:\u003C/p>\n\u003Cdiv class=\"code-block\">\u003Cdiv class=\"controls\">\u003Cbutton class=\"copy-to-clipboard raised\" title=\"Copy to clipboard\" aria-label=\"Copy to clipboard\">\u003C/button>\u003C/div>\u003Cpre data-js data-ts data-language=\"svelte\" class=\"shiki css-variables\" style=\"background-color:var(--shiki-background);color:var(--shiki-foreground)\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\"><\u003C/span>\u003Cspan style=\"color:var(--shiki-token-string-expression)\">script\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">>\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">x\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">0\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-keyword)\">let\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">y\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-token-constant)\">0\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">z\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">y;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">setY\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">(x);\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-keyword)\">function\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">setY\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">(value) {\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\t\u003Cspan style=\"color:var(--shiki-foreground)\">y\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">value;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-foreground)\">}\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\"></\u003C/span>\u003Cspan style=\"color:var(--shiki-token-string-expression)\">script\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">>\u003C/span>\u003C/span>\u003C/code>\u003C/pre>\u003C/div>\u003Ch2 id=\"Browser-only-code\">\u003Cspan>Browser-only code\u003C/span>\u003Ca href=\"#Browser-only-code\" class=\"permalink\" aria-label=\"permalink\">\u003C/a>\u003C/h2>\u003Cp>Reactive statements run during server-side rendering as well as in the browser. This means that any code that should only run in the browser must be wrapped in an \u003Ccode>if\u003C/code> block:\u003C/p>\n\u003Cdiv class=\"code-block\">\u003Cdiv class=\"controls\">\u003Cbutton class=\"copy-to-clipboard raised\" title=\"Copy to clipboard\" aria-label=\"Copy to clipboard\">\u003C/button>\u003C/div>\u003Cpre data-js data-ts data-language=\"js\" class=\"shiki css-variables twoslash lsp\" style=\"background-color:var(--shiki-background);color:var(--shiki-foreground)\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\">$\u003C/span>\u003Cspan style=\"color:var(--shiki-token-punctuation)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-keyword)\">if\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">(\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">browser\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">) {\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\t\u003Cspan style=\"color:var(--shiki-token-constant)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-constant)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-keyword)\">var\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">document\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">:\u003C/span> \u003Cspan style=\"color:var(--shiki-token-function)\">Document\u003C/span>\u003C/code>\u003Cdiv class=\"twoslash-popup-docs\">\u003Cp>\u003Cstrong>\u003Ccode>window.document\u003C/code>\u003C/strong> returns a reference to the document contained in the window.\u003C/p>\n\u003Cp>\u003Ca href=\"https://developer.mozilla.org/docs/Web/API/Window/document\">MDN Reference\u003C/a>\u003C/p>\n\u003C/div>\u003C/span>document\u003C/span>\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">.\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">\u003Cspan class=\"twoslash-hover\">\u003Cspan class=\"twoslash-popup-container\">\u003Ccode class=\"twoslash-popup-code\">\u003Cspan style=\"color:var(--shiki-token-constant)\">Document\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">.title: string\u003C/span>\u003C/code>\u003Cdiv class=\"twoslash-popup-docs\">\u003Cp>The \u003Cstrong>\u003Ccode>document.title\u003C/code>\u003C/strong> property gets or sets the current title of the document. When present, it defaults to the value of the \u003Ctitle>.\u003C/p>\n\u003Cp>\u003Ca href=\"https://developer.mozilla.org/docs/Web/API/Document/title\">MDN Reference\u003C/a>\u003C/p>\n\u003C/div>\u003C/span>title\u003C/span>\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-token-keyword)\">=\u003C/span> \u003Cspan style=\"color:var(--shiki-foreground)\">\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">title\u003C/span>\u003Cspan style=\"color:var(--shiki-foreground)\">;\u003C/span>\u003C/span>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:var(--shiki-foreground)\">}\u003C/span>\u003C/span>\u003C/code>\u003C/pre>\u003C/div>",sections:[{slug:"Understanding-dependencies",title:"Understanding dependencies",subsections:[]},{slug:"Browser-only-code",title:"Browser-only code",subsections:[]}],children:[],prev:{slug:"docs/svelte/legacy-let",title:"Reactive let/var declarations"},next:{slug:"docs/svelte/legacy-export-let",title:"export let"}},related:void 0},uses:{params:["topic","path"],url:1}}], form: null, error: null }); }); } </script> </div> <script> if (localStorage.getItem('sv:show-legacy') === 'open') { for (const node of document.querySelectorAll('details.legacy')) { node.open = true; } } if (localStorage.getItem('svelte:prefers-ts') === 'false') { for (const node of document.querySelectorAll('.ts-toggle')) { node.checked = false; } } </script> </body> </html>