JavaScript Code

How to split string at commas?


Problem Statement

given a string, split the string with comma character as separator

Solution

to split string str at new lines, call split() function on the string, and pass comma character as argument to split() function

str.split(',')

Example

1. split string at commas

let name = 'apple,banana,cherry';
let output = name.split(',');
console.log(output);