tpoker
tpoker

Reputation: 502

parameter?: type | undefined vs parameter?: type

I'm trying to understand the difference between two ways of declaring an optional parameter for a function:

parameter?: type

and

parameter?: type | undefined

From what I understand, the first is a shorthand for

parameter: type | undefined = undefined

Is the second way any different from this?

Thanks!

Upvotes: 2

Views: 726

Answers (1)

Kouta Nakano
Kouta Nakano

Reputation: 643

An optional parameter resolves its parameter to undefined if nothing has been passed.

Therefore, the second statement is redundant as it is an optional parameter but also allows 'type' and undefined.

No difference in what it does as long as type is strictly typed.

Upvotes: 1

Related Questions