CodesJava

Easy learning with example program codes

typescript function optional parameter


A TypeScript function is a group of statements which is used for performing a specific task. It provides the facility of code re-usability.

Every parameter is assumed to be required by the function, in TypeScript. In short, the number of parameters given to a function at function call has to match the number of parameters in the function definition. We can mark a parameter optional by appending a question mark to its name. Optional parameter should be the last parameter of the function.

Typescript function optional parameter syntax:

function function_name (param1:datatype, param2?:datatype)

Typescript function optional parameter example:

function dispDetails(fname:string,lname?:string) {  
   console.log("Fisrt Name",fname); 
 
   if(lname!=undefined)  
   console.log("Last Name",lname); 
}
dispDetails("Asmita");
dispDetails("Nidhi", "Gupta");

Try it:

JS Bin on jsbin.com
 

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki