diff --git a/documentation/docs/01-introduction/xx-props.md b/documentation/docs/01-introduction/xx-props.md index cad854d8785d..5266c635e8b1 100644 --- a/documentation/docs/01-introduction/xx-props.md +++ b/documentation/docs/01-introduction/xx-props.md @@ -26,6 +26,28 @@ You can specify a fallback value for a prop. It will be used if the component's ``` +> [!NOTE] If a prop is explicitly passed as null, the default value will not be used, and null will be assigned instead. However, if the prop is undefined or not provided at all, the default value will be used. + +```svelte + + +

The answer is {answer}

+``` + +```svelte + + + + + + + +``` + To get all properties, use rest syntax: ```svelte diff --git a/documentation/docs/03-template-syntax/01-basic-markup.md b/documentation/docs/03-template-syntax/01-basic-markup.md index b41dc187c337..3a9f5c72caa2 100644 --- a/documentation/docs/03-template-syntax/01-basic-markup.md +++ b/documentation/docs/03-template-syntax/01-basic-markup.md @@ -72,6 +72,29 @@ When the attribute name and value match (`name={name}`), they can be replaced wi --> ``` +When passing null or undefined to an attribute, the attribute is omitted from the rendered HTML. + +```svelte + + +
Attributes are not included.
+ +``` + +If an empty string ("") is assigned to an attribute, the attribute remains in the HTML but with an empty value. + +```svelte + + +
Hello
+ +``` + ## Component props By convention, values passed to components are referred to as _properties_ or _props_ rather than _attributes_, which are a feature of the DOM. @@ -154,6 +177,43 @@ A JavaScript expression can be included as text by surrounding it with curly bra {expression} ``` +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: + +- Primitive values (numbers, booleans, strings) are directly converted to strings. +- Objects are converted based on JavaScript’s type coercion rules: + + - 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. + - 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]". + - 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. + - 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. + +- undefined and null are treated as empty strings (""). +- Expressions using runes ($state, $derived, etc.) maintain their specific reactive behavior. + +```svelte + let emptyStr = ""; + let num = 1; + let bool = false; + let obj1 = { key: "value" }; + let obj2 = { toString: () => "str" }; + let obj3 = { [Symbol.toPrimitive]: () => "primitive" }; + let obj4 = { valueOf: () => 42, toString: () => "str" }; + let obj5 = { [Symbol.toStringTag]: "CustomObject" }; + let empty = undefined; + let nul = null; + +

{emptyStr}

+

{num}

+

{bool}

+

{obj1}

+

{obj2}

+

{obj3}

+

{obj4}

+

{obj5}

+

{empty}

+

{nul}

+``` + 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 `}`. If you're using a regular expression (`RegExp`) [literal notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor), you'll need to wrap it in parentheses.