typescript
/

TypeScript Optional & Default Parameters

Last Sync: Today

On this page

9
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Optional & Default Parameters

What are Optional Parameters?

Optional parameters allow you to define function parameters that may or may not be provided when the function is called.

Optional Parameter Syntax

TypeScriptRead-only
1
function greet(name?: string) {
  return name || 'Guest';
}

Default Parameters

Default parameters assign a default value when no argument is passed.

TypeScriptRead-only
1
function greet(name: string = 'Guest') {
  return name;
}

Optional vs Default

FeatureOptionalDefault
Value RequiredNoNo
Default ValueNoYes
UsageFlexible inputPredefined fallback

Combining Both

TypeScriptRead-only
1
function greet(name?: string, age: number = 18) {
  return `${name || 'Guest'} - ${age}`;
}

Parameter Order Rules

Optional parameters must come after required parameters, while default parameters can be placed flexibly but should follow best practices.

Best Practices

  • Place optional parameters at the end
  • Use default values for predictable behavior
  • Avoid too many optional parameters
  • Keep function signatures simple

Common Mistakes

  • Wrong parameter order
  • Confusing optional with default
  • Overusing optional parameters
  • Not handling undefined values properly

Conclusion

Optional and default parameters improve flexibility in TypeScript functions while maintaining clarity and type safety.

Try it yourself

function greet(name: string = 'Guest') {
  return name;
}

Test Your Knowledge

Q1
of 3

Optional symbol?

A
*
B
?
C
!
D
#
Q2
of 3

Default value means?

A
Required
B
Fallback
C
Error
D
Type
Q3
of 3

Order rule?

A
Optional first
B
Optional last
C
Any order
D
No rule

Frequently Asked Questions

What is optional parameter?

A parameter that may not be provided.

What is default parameter?

A parameter with a default value.

Which is better?

Default parameters are more predictable.

Previous

ts function types

Next

ts rest params

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.