Performance: Text interpolation with {}
Text interpolation affects how quickly dynamic text content appears on the page and how often the DOM updates when data changes.
<script> let name = 'Alice'; $: message = `${name} is logged in`; </script> <p>{message}</p>
<script> let name = 'Alice'; </script> <p>{name + ' is logged in'}</p>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct string concatenation in interpolation | Updates text node on each change | 1 reflow per update | 1 paint per update | [X] Bad |
| Precomputed string variable interpolation | Updates text node only when variable changes | 1 reflow per update | 1 paint per update | [OK] Good |
name inside your HTML?{} to insert variables or expressions inside HTML. To display the value of name, write {name} inside the HTML.2 + 3 inside a Svelte component?{} to be evaluated and shown. Only {2 + 3} correctly uses curly braces for expression evaluation; others are invalid.<script>
let count = 5;
</script>
<p>Count is: {count * 2}</p>count is 5, and count * 2 equals 10, so "Count is: 10" is displayed.<script>
let message = 'Hello';
</script>
<p>{message</p>{ but lacks closing } after message. Proper syntax is {message}.firstName and lastName inside a Svelte component. Which code correctly shows "Full Name: John Doe" if firstName = 'John' and lastName = 'Doe'?{}. Use +: {firstName + ' ' + lastName} gives "John Doe". Commas invoke comma operator (returns lastName), & is bitwise, concat(lastName) gives "JohnDoe" (no space).Pass the test and this pattern moves unknown