Default parameters with types
📖 Scenario: You are creating a simple greeting function for a website. Sometimes users provide their name, and sometimes they don't. You want to make sure the function works well in both cases.
🎯 Goal: Build a TypeScript function called greet that takes a name parameter with a default value and a type. The function should return a greeting message.
📋 What You'll Learn
Create a function named
greet with a parameter name of type stringSet the default value of
name to "Guest"Return a greeting string using the
name parameterCall the function twice: once without arguments and once with the argument
"Alice"Print the results of both calls
💡 Why This Matters
🌍 Real World
Default parameters help make functions flexible and easier to use when some information is optional.
💼 Career
Understanding default parameters with types is important for writing clean, safe, and maintainable TypeScript code in real projects.
Progress0 / 4 steps