
 258 views
How to Convert Comma Separated String into an Array in JavaScript
To convert a comma-separated string into an array in JavaScript, you can use the split() method. The split() method splits a string into an array of substrings based on a specified separator.
Here’s an example:
JavaScript
var str = "apple,banana,orange";
var arr = str.split(",");
console.log(arr);Output:
JavaScript
["apple", "banana", "orange"]In this example, the split(",") method is used on the string str, specifying a comma (“,”) as the separator. This splits the string into an array at each occurrence of a comma, creating an array arr with three elements: “apple”, “banana”, and “orange”.
You can then use the resulting array for further processing or manipulation as needed.