There are many ways to split a string by space or comma in Javascript. You can use String.prototype.split(), combine split(), and trim(),… Learn more about it by following the examples below to understand the solutions better.
Split a string by space or comma in JavaScript
String.prototype.split()
Use a split method by passing it a separator to split a string. This method returns an array of substrings. The separator could be a string or a regular expression object. If a separator is undefined, the result will be an array containing one element, with the element at position 0 being the entire string.
You can learn more about how to split a string with multiple separators.
Syntax:
yourStr.split()
yourStr.split(separator)
Example:
let myStr = "Hello, my name is Otis Manders"; let regObj = /[\s,]+/; myStr = myStr.trim(); // remove leading and trailing whitespace from string let splitMyStr= myStr.split(regObj); console.log(splitMyStr);
Output
["Hello","my","name","is","Otis","Manders"]
Explain
In the example above, the separator is a regular expression object. The regular pattern includes all separators you want to split. Commas and spaces are used in this example. You can use reg pattern /[\s,]+/ to include all special cases of whitespace characters like tab, space,…but /[ ,]+/ is ok if your string contains only space characters. Before splitting, use the trim() method to remove whitespace from both sides of a string if you don’t want the result to appear with an empty string (zero length) at the first (or last) position of the returned array.
split() and trim()
Use the split() method to split and the trim() method to remove surrounding spaces.
Example
let myStr = "Hello, my name is Otis Manders"; let splitMyStr= myStr.split(/[ ,]+/).map(value => value.trim()); console.log(splitMyStr);
Output
["Hello","my","name","is","Otis","Manders"]
Explain
In this case, split the string by passing /[ ,]+/ separator into a split() method. With the returned array, iterate over each element in the array by the map() method to remove whitespace from both sides.
split() and filter()
Use the split() method to split and use the filter() method to remove falsy values in JavaScript.
Falsy values could be: false, “”, ”, null, undefined, NaN (not a number),…
Example:
let myStr = "Hello, my name is Otis Manders"; let regObj = /[\s,]+/; let splitMyStr= myStr.split(regObj); splitMyStr = splitMyStr.filter(element => element); console.log(splitMyStr);
Output
["Hello","my","name","is","Otis","Manders"]
Explain
The above case splits the string by passing /[\s,]+/ separator into a split() method. With the returned array, loop through each element in the array by the filter() method to return only truthy values.
Truthy values are all values that are not falsy.
Summary
Some solutions above are convenient ways to help you split a string by space or comma in Javascript. However, the split() method with /[\s,]+/ separator will be the best for all situations. If you have any questions, please feel free to leave us a comment below.

My name is Otis Manders, and I like programming and sharing my skills. Java, Javascript, and others are some of my strong programming languages that I can share with everyone. In addition, I have created applications with the React library, HTML, CSS, and Javascript.
Name of the university: UTC
Programming Languages: C, C++,Java, Javascript, Html, Css