Rest parameters let you collect many values into one place easily. This helps when you don't know how many inputs you will get.
Rest parameters in Svelte
function example(...rest) { // rest is an array of extra arguments } <script> // $$restProps collects undeclared props into an object </script>
In functions, rest parameters use three dots ... before a name to gather extra arguments into an array.
In Svelte components, $$restProps automatically collects all props that are not explicitly declared with export let.
function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); }
name prop and collects all other props in $$restProps.<script> export let name; </script> <p>Hello {name}!</p> <p>Extra props: {JSON.stringify($$restProps)}</p>
This Svelte component defines a function greet that takes one fixed argument and then any number of names using rest parameters. It joins the names with 'and' and shows the greeting.
<script> // Function using rest parameters function greet(greeting, ...names) { return `${greeting} ${names.join(' and ')}`; } let message = greet('Hello', 'Alice', 'Bob', 'Charlie'); </script> <h1>{message}</h1>
Rest parameters must be the last parameter in a function.
Rest parameters gather arguments into an array, so you can use array methods on them.
In Svelte, rest props help pass unknown or extra props to child components or elements.
Rest parameters collect many arguments into one array.
They make functions and components flexible with inputs.
In Svelte, rest props help handle extra properties easily.