CodesJava

Easy learning with example program codes

typescript rest parameters tutorial


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

When we talked about optional and default parameters all have one common thing, they talk about one parameter at a time. Sometimes we need multiple parameters as a group. TypeScript rest parameters used in such situations. They are act as the placeholder for multiple arguments of the same type. Use the ellipsis (…) as the prefix of rest parameter.

Typescript rest parameters syntax:

function function_name (…param1:datatype)

Typescript rest parameters example:

function showNames(...names:string[]) {    
   for(var i = 0;i<names.length;i++) { 
      console.log(names[i]);
   } 
} 
showNames("Rajeev","Gunjan","Vikram","Asmita"); 
showNames("Mahesh", "Jai", "Narender", "Vishal", "Hemant");

Try it:

JS Bin on jsbin.com
 

Please follow and like us:


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