EN
JavaScript - convert array of strings to comma separated string
1
answers
5
points
Is there easy way to convert array of string to comma separated string in JavaScript?
I mean without using for loop and string concatination.
I know I can do this like this:
// ONLINE-RUNNER:browser;
var arr = ['en', 'de', 'pl'];
var text = '';
for (var i = 0; i < arr.length; i++) {
text += arr[i];
if (i !== arr.length - 1) {
text += ',';
}
}
console.log(text); // en,de,pl
Output:
en,de,pl
Is there easier way to do this?
1 answer
3
points
The best and quickes way it to use array.join(",")
method.
Quick example with 1 liner solution:
// ONLINE-RUNNER:browser;
var arr = ['en', 'de', 'pl'];
var text = arr.join(",");
console.log(text); // en,de,pl
Output:
en,de,pl
0 comments
Add comment