Make array from comma separated string in JavaScript

To make an array from string separated with commas, use the split() function.

array.split(',');

Example:

<script>
    var string = 'Harry,Walter,Robin';
    var array = string.split(',');

    console.log(array);
</script>

Output (console):

['Harry', 'Walter', 'Robin']

Example 2:

Make an array from string separated with commas and spaces.

<script>
    var names = 'Anna, Monica, Lauren';
    var array = names.split(', ');

    console.log(array);
</script>

Output (console):

['Anna', 'Monica', 'Lauren']