Convert Array to Comma Separated String JavaScript

Convert Array to Comma Separated String JavaScript

js has several functions such as toString(), join(), and for loop to convert an array to a comma separated string; In this tutorial, we will show you how to convert an array to comma separated string in javascript.

Or if you want to convert strings to arrays in javascript. You can read this post – https://www.tutsmake.com/javascript-convert-string-to-array-javascript/.

How to Convert Array to Comma Separated String JavaScript

Here are multiple methods and approaches to convert array and object to comma delimited string to array javascript:

  • toString() method
  • join() method
  • For Loop method
  • 3 Approaches to achieve this

toString() method

The javascript toString() method is a simple way to convert an array to comma separated string.

Basic Syntax:

array.toString();

join() Method

The join() method creates and returns a new string by concatenating all of the elements in an array with a specified separator, such as a comma.

Note: The elements of the array will be separated by a specified separator.
If not specified, Default separator comma (, ) is used.

Syntax:

array.join(separator) 

For Loop method

The for loop helps us to manually concatenate array elements with commas string.

3 Approaches to achieve this

Approach 1: Convert array to comma separated string using toString() method

Here is an example of how to use the toString() method to convert an array to a comma-separated string:

 var lang = ['php','javascript','python', 'c', 'c+', 'java'];
 document.write( "JavaScript - convert an array into comma-separated strings are :-" + lang.toString() );

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Convert Array into Comma-Separated String with toString() Method</title>
</head>
<body>
  <script type = "text/javascript">
  
  var lang = ['php','javascript','python', 'c', 'c+', 'java'];
  document.write( "JavaScript - convert an array into comma-separated strings are :-" + lang.toString() );
  </script>  
</body>
</html>

Result of the above code is:

JavaScript Convert Array into Comma-Separated String

Approach 2: Convert Array to String javascript using join Method

Here is an example of how to use the join() method to convert an array to a comma-separated string:

var lang = ['php','javascript','python', 'c', 'c+', 'java'];
document.write( "JavaScript - convert an array into comma-separated strings are :-" + lang.join(',') );

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Convert Array into Comma-Separated String With join() Method</title>
</head>
<body>
  <script type = "text/javascript">
  
  var lang = ['php','javascript','python', 'c', 'c+', 'java'];
  document.write( "JavaScript - convert an array into comma-separated strings are :-" + lang.join(',') );
  </script>  
</body>
</html>

Result of the above code is:

JavaScript Convert Array into Comma-Separated String With join() Method

Approach 3: convert array to comma separated string javascript using for loop

Here is an example of how to use the for loop() method to convert an array to a comma-separated string:

// Sample array
const myArray = ["apple", "banana", "orange", "grape"];

// Convert array to comma-separated string using a for loop
let commaSeparatedString = "";
for (let i = 0; i < myArray.length; i++) {
  commaSeparatedString += myArray[i];
  if (i < myArray.length - 1) {
    commaSeparatedString += ", ";
  }
}

// Display the result
console.log("Comma-separated string:", commaSeparatedString);

Conclusion

These three methods offer different approaches to converting an array to a comma-separated string in JavaScript. Choose the one that suits your coding style and the specific requirements of your project.

Recommended JavaScript Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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