Navigate back to the homepage

How to convert array of strings to comma separated string in Javascript?

Infinitbility
JavaScript
May 15th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, we are going to learn how can we convert arrays which have store strings to comma (,) separated strings, here we will use the javascript join method to do this.

Table of content

  1. Introduction to join() method
  2. join() method example
  3. Different separator example

Let’s start today’s topic How to convert an array of strings to comma separated strings in Javascript?


Introduction to join() method

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

— MDN


join() method example

Here, we will see how we can use the javascript join() method to convert an array of strings to comma-separated strings.

let’s see what we can do.

1const elements = ['Fire', 'Air', 'Water'];
2
3console.log(elements.join());
4// expected output: "Fire,Air,Water"

Here, we have to just use the join method on our array and it will return array elements separated by a comma.

JavaScript, array join example
JavaScript, array join example

Different separator example

Where, we will see how we can use the javascript join() method to convert an array of strings to space, hyphen ( - ) separated strings.

let’s see what we can do.

1const elements = ['Fire', 'Air', 'Water'];
2
3console.log(elements.join(' '));
4// expected output: "Fire Air Water"
5
6console.log(elements.join('-'));
7// expected output: "Fire-Air-Water"

Here, we have to just use the join method on our array and pass separator as a parameter, it will return array elements separated by parameter.

JavaScript, array join parameter example
JavaScript, array join parameter example

All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to remove duplicates from array of strings in javascript?

Example of remove duplicates from array of strings in javascript with step by step guide.

May 15th, 2022 · 1 min read

How to reverse array without mutating in javascript?

Example of array reverse without mutating in javascript with step by step guide.

May 15th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]