You can use the split() method in JavaScript to convert a comma separated string into an array. The split() method takes a delimiter as an argument, and it returns an array of substrings by splitting the original string at each occurrence of the delimiter.

For example:

let str = "apple,banana,orange";
let fruits = str.split(",");
console.log(fruits); // ["apple", "banana", "orange"]

You can also use other delimiter besides comma, like:

let str = "apple|banana|orange";
let fruits = str.split("|");
console.log(fruits); // ["apple", "banana", "orange"]

You can also use the split() method without any argument which will split the string by whitespaces

let str = "apple banana orange";
let fruits = str.split();
console.log(fruits); // ["apple", "banana", "orange"]

Note that the original string is not modified, the split() method returns a new array.

Also Read:

Categorized in: