A concat() function used to concatenate the string and generate single string by concatenating all specified strings. A JavaScript allows to concatenate strings by using plus (+) operator.
1 <source_string>.concat(<comma_separated_strings>);
source_string : A source string that needs to concatenate with specified strings
1 let str = "Hello";
2
3 str.concat(" ", "World...");
In the above example, we have define string with initial value and by using concat() function we are concatenate other two string by passing it into the parameter. It returns new concatenated string.
concat() with string array
concat() with string array
1 function concatenate(array) {
2 let res = ""
3 for(str of array) {
4 res = res.concat(" ", str);
5 }
6 return res;
7 }
8
9 let arr = ["this", "is", "string", "functions"]
10
11
12 let res = print(arr)
13 console.log("Concatenated strings : ");
14 console.log(res);
In the above example, an array of string is define and initialized with string literals. A concatenate() function is define that accepts array of string in parameter. An array is iterated using for...of loop, it iterate each elements and concatenate with string res separated by white space. A resulting string is returned from function that assigned to the variable and printed.
1 Concatenated strings :
2 this is string functions
Related options for your search