Top community members

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

JavaScript - convert array of strings to comma separated string

2 points
Asked by:
Ticket Ninja
2332

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 answers
3 points
Answered by:
Ticket Ninja
2332

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

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more