Optional Parameters in Svelte Functions
📖 Scenario: You are building a simple Svelte component that greets users. Sometimes you want to greet with just a name, and other times you want to add a friendly message. You will learn how to use optional parameters in a function inside a Svelte component to handle this.
🎯 Goal: Create a Svelte component with a function that takes a required name parameter and an optional greeting parameter. If greeting is not provided, it should default to "Hello". Display the full greeting message in the component.
📋 What You'll Learn
Create a Svelte component with a function named
greet that takes name and optional greeting parametersSet the default value of
greeting to "Hello"Call the
greet function with and without the optional parameterDisplay the greeting messages in the component
💡 Why This Matters
🌍 Real World
Optional parameters let you write flexible functions that work well in many situations without needing many versions of the same function.
💼 Career
Understanding optional parameters is important for writing clean, reusable code in frameworks like Svelte, which is widely used for building interactive web apps.
Progress0 / 4 steps