I need to implement a string concatenate function, the functionality shows below,
function concatenation(original: string, name1?, name2?){
return original + name1 + name2;
}
However, the question is, there can be more parameters about name
; therefore, this is a not a good coding style. I want to change its like shows below.
function concatenation(original: string, name?:[string]){
return original + ...name1;
}
But in this way, the program will report an error. How could I achieve it?
You can use rest with join as:
function concatenation(original: string, name1?, name2?) {
return original + name1 + name2;
}
concatenation('Original', 'Name1', 'Name2', 'Name3');
This can handle any number of string arguments
function concatenation2(...names: string[]) {
return names.join('');
}
concatenation2('Original', 'Name1', 'Name2', 'Name3');
function concatenation2(...names) {
return names.join('');
}
console.log(concatenation2('Original', 'Name1', 'Name2', 'Name3'));