- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
Optional Parameters and Default Parameters
This lesson covers parameters that can be omitted (optional parameters) and parameters with a built-in initial value (default parameters). It's for anyone searching "TypeScript optional parameter default value."
Adding ? after a parameter name makes it "okay to omit." Writing paramName: type = initialValue instead sets a default value used whenever the caller doesn't pass one.
The example sets a default greeting with greet(name: string, greeting: string = "Hello"). The caller can either omit the greeting or specify one.
A common early mistake is confusing when to use an optional parameter (?) versus a default parameter. If you want a missing value to be treated as undefined, use ?; if you want a fallback value to be filled in automatically, use a default value โ that distinction is worth remembering.
In real projects, the more configuration options a function has, the more setting common values as defaults keeps the caller's code simple.
๐งช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
