By 06/07/2015 11 Comments

TypeScript Optional Parameter List

Optional Parameter List:

  • In TypeScript, the all parameters of a function are required by default.
  • In JavaScript if we do not supply the full parameters to the functions then the parameter value will be undefined and it returns text “undefined”.
  • Using TypeScript language if we do not supply all parameters to a function the compiler will generate a compile-time error.
  • TypeScript has a “?” question mark symbol which indicates the optional parameters in a function.
  • By using question mark “?” symbol TypeScript compiler does not generate the compile-time error.
  • Through question mark symbol “?” we can define unlimited number of optional parameters in function parameter-list.
  • Remember, the optional parameters must be declared as the last parameters in function parameter-list.

Example:

/* A string type anonymous function declaration which has 4 parameters. Notice that last two parameters (php and html) are marked as optional by “?” question mark symbol. */
var pLanguages = function (asp: string, js: string, php?: string, html?: string): string {
    return (asp + " , " + js + " , " + php + " , " + html);
};

/* Call and print the above anonymous function, 2 parameters are passed from it. Other 2 are optional parameters. */
document.write(pLanguages("Asp.net", "JavaScript"));






← Previous Tutorial


Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .