You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/03-template-syntax/01-basic-markup.md
+22-10Lines changed: 22 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -179,9 +179,15 @@ A JavaScript expression can be included as text by surrounding it with curly bra
179
179
180
180
When using {expression} inside markup, Svelte automatically converts the value to a string before rendering it and makes the expression reactive (similar to wrapping it in $derived). The conversion follows JavaScript's standard behavior:
181
181
182
-
- Primitive values (number, boolean, string) are directly converted to strings.
183
-
- Objects call their .toString() method (if not overridden, it defaults to [object Object]).
184
-
- Undefined and null are treated as empty strings ("").
182
+
- Primitive values (numbers, booleans, strings) are directly converted to strings.
183
+
- Objects are converted based on JavaScript’s type coercion rules:
184
+
185
+
- If an object defines a [Symbol.toPrimitive](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive) method, it takes precedence and determines how the object is converted.
186
+
- Otherwise, if a [toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) method is present, it is called. If not overridden, it defaults to "[object Object]".
187
+
- If toString() is not available or does not return a primitive, JavaScript may fall back to [valueOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf), which may be used if it returns a primitive value.
188
+
- Additionally, an object with a [Symbol.toStringTag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property affects how it is represented when coerced to a string.
189
+
190
+
- undefined and null are treated as empty strings ("").
185
191
- Expressions using runes ($state, $derived, etc.) maintain their specific reactive behavior.
186
192
187
193
```svelte
@@ -190,16 +196,22 @@ When using {expression} inside markup, Svelte automatically converts the value t
190
196
let bool = false;
191
197
let obj1 = { key: "value" };
192
198
let obj2 = { toString: () => "str" };
199
+
let obj3 = { [Symbol.toPrimitive]: () => "primitive" };
Curly braces can be included in a Svelte template by using their [HTML entity](https://developer.mozilla.org/docs/Glossary/Entity) strings: `{`, `{`, or `{` for `{` and `}`, `}`, or `}` for `}`.
0 commit comments