Stay connected with us on X, Discord, and LinkedIn to stay up to date with future analysis
Header image for Svelte 5

Svelte 5

Can the models correctly write Svelte 5 components? Do they avoid using patterns from earlier versions?

Prompt

Write a counter tool using Svelte 5. It must increment the number when the button is pressed. It must display whether the count value is even or odd. And it must log to the console every time the button is pressed. Return only the Svelte component, fenced in by triple backticks (```).

Answer guidance

This sort of thing (except more likely to be all in one file). ``` // counter.svelte.js export function createCounter(initialCount = 0) { let count = $state(initialCount); let doubled = $derived(count * 2); let isEven = $derived(count % 2 === 0); function increment() { count += 1; } $effect(() => { console.log(`Count changed to ${count}`); }); return { get count() { return count; }, get doubled() { return doubled; }, get isEven() { return isEven; }, increment }; } // App.svelte <script> import { createCounter } from './counter.svelte.js'; const counter = createCounter(5); </script> <button onclick={counter.increment}> Count: {counter.count} (Doubled: {counter.doubled}, {counter.isEven ? 'Even' : 'Odd'}) </button> ```

Drag to resize
Drag to resize
Drag to resize
Drag to resize
Drag to resize
Drag to resize
Drag to resize
Drag to resize