Convert Space Separated string to Array in JavaScript

Use the split() method.

yourString.split(" ");

Example:

Let’s say we have a string with some words separated with spaces. In many cases, it would be more comfortable to store it in the array. We just need to convert the string to the array with the split().

<script>
    var words = 'Mango Car Wallet Headphones';
    var array = words.split(" ");

    console.log(array);
</script>

Output: (4) [‘Mango’, ‘Car’, ‘Wallet’, ‘Headphones’]