Wednesday, November 7, 2018

TypeScript - ES6 - providing default parameters to functions

We can set default parameters to functions in TypeScript. Consider the following function:

const myCountFunc = (begin:number = 10): void => {
console.log("begin = "+begin);
for (let i=begin; i > 0; i--){
begin--;
console.log("begin = "+begin);

}
console.log("Finished");
}

myCountFunc();

During the function call, since, we have not provided any input parameter number. It will start from the default parameters, which is 10.

Console Output is as follows:

begin = 10
city-App.ts:46 begin = 9
city-App.ts:46 begin = 8
city-App.ts:46 begin = 7
city-App.ts:46 begin = 6
city-App.ts:46 begin = 5
city-App.ts:46 begin = 4
city-App.ts:46 begin = 3
city-App.ts:46 begin = 2
city-App.ts:46 begin = 1
city-App.ts:46 begin = 0
city-App.ts:49 Finished




No comments:

Post a Comment