TypeScript : Functions : Default Parameters

Problem

I want a parameter to have a default value in my TypeScript function.

Answer

You can assign a value to a parameter in your parameter list. The value can be any type of object and even functions.

Example


MyFunction(myString: string, myDefaultValue = "This is a string": string) { ... }


MyFunction(myNumber: number, myDefaultValue = getDefaultValue(): any) {...} //could be : string, or anything else