Svelte · Junior · Question 11 of 76

How do you declare a value computed from other state?

let count = $state(2);
let doubled = $derived(count * 2);

Explanation

Component code runs once on creation, so a plain let would keep the initial value. Updating state inside $effect for this is an anti-pattern.

Further reading

Problem with a question

Svelte · Junior · question 11

Test yourself

Take the Svelte · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz