Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » How to convert array to comma separated strings in javascript

How to convert array to comma separated strings in javascript

August 11, 2022 by Source Freeze Leave a Comment

To Convert array to comma separated strings in Javascript first need to pass the array to the String object like String(arr), then the String(arr) will return the comma separated strings as an output.

☞ Check out The Complete JavaScript Course 2023: From Zero to Expert! (Buy now, the offer ends in a few hours)

Please refer to the below example here we have passed the arrValue=[“javascript”,”typescript”,”react”,”angular”] to the String object, then the String object will return the values as comma separated output like ‘javascript,typescript,react,angular’

The String object is used to represent and manipulate a sequence of characters.

let arrValue = ['javascript', 'typescript', 'react', 'angular'];

 let strValue = String(arrValue);

 console.log(strValue); //  'javascript,typescript,react,angular'

 console.log(typeof strValue); //  string

Alternatively, we can use the Array.prototype.join() method, this Array.join() method is used to join() the all the array with the specified characters.

To convert array to comma separated strings in javascript we can use the join() method of an array, by passing the comma as a parameter. the Array.join() method returned the comma separated strings as an output.

Please refer to the below example we have passed the arrValue=[“javascript”,”typescript”,”react”,”angular”] to the arrValue to join() method of array, then join() method will return the values as comma separated output. e.g ‘javascript,typescript,react,angular’

The join() method creates and returns a new string by concatenating all of the elements in an array

let arrValue = ['javascript', 'typescript', 'react', 'angular'];

 const strValue = arrValue.join(',');

 console.log(strValue); // 'javascript,typescript,react,angular'

 console.log(typeof strValue); //  string

the Array.prototype.join() will take the specified separator as the parameter in our case we have used a comma as a separator but if want to use a different separator we can use it it will be separated based on the specified separator character for example space, #, or whatever you want.

**************

Related Post: Convert Array to Space Separated Strings

*************

Array to comma separated strings in JavaScript

☞ Check out The Complete JavaScript Course 2023: From Zero to Expert! (Buy now, the offer ends in a few hours)

Filed Under: javascript Tagged With: javascript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Comments

    Tags

    beginner colon cross-platform es6 function html ionic iOS javascript mobile application development nextjs objective-c swift switch ternary typescript uiwebview Visual Studio plugin web developer

    Copyright © 2025 Source Freeze