Share and Learn To Write Code!

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

Share with   

How to convert string which is having comma separated list into array list in javascript

Here we are converting comma separated string into array list using JavaScript. So we can use this java script in every framework like php, angular, react, vue js etc.
Java Script   JS   JavaScript Array   JavaScript String   HTML  

Converting comma separated string into array list using JavaScript having 2 methods i.e.

1) Using split() method

2) Iterating with string and slicing string to comma and push to array.


Method 1 : Using split() method

Syntax for converting array

var temp_string = "One, Two, Three, Four, Five"
var temp_arr = new Array();
temp_arr = res.CustomerAssignGroupIds.split(", ");

Check Sample HTML Demo

<!DOCTYPE html> 
  	   Convert comma separated string to array using JavaScript 
  	<h1 style="color: blue"> ProCodeProgramming </h1> 
  	<b>Convert comma separated string 
  	to array using JavaScript</b> 
  	<p>Original string is "One, Two, Three, Four, Five"</p> 
  	<p>Separated Array is: <span class="output"></span></p> 
  	<button onclick="separateString()"> 
  	      Remove Text 
  	<script type="text/javascript"> 
  		function separateString() { 
  			originalString = "One, Two, Three, Four, Five"; 
  			separatedArray = originalString.split(', '); 
  			document.querySelector('.output').textContent = 


Method 2 : Iterating with string and slicing string to comma and push to array.

Syntax for slicing string using for loop

originalString = "Bhushan, Amit, Maya, Pratik, Shanaya"; 
separatedArray = []; 
// index of end of the last string  
let previousIndex = 0; 
for(i = 0; i < originalString.length; i++) { 
    // check the character for a comma 
    if (originalString[i] == ', ') { 
        // split the string from the last index 
        // to the comma 
        separated = originalString.slice(previousIndex, i); 
        // update the index of the last string 
        previousIndex = i + 1; 
// push the last string into the array 
separatedArray.push(originalString.slice(previousIndex, i));

Thank you for watching article. If you want learn more about java script please check our javascript tutorials.

Leave a reply
Registerd Users
Total Subscribers
Total Articles
Total Views