const array = str.split(',');

MDN reference, mostly helpful for the possibly unexpected behavior of the limit parameter. (Hint: "a,b,c".split(",", 2) comes out to ["a", "b"], not ["a", "b,c"].)

Answer from Matchu on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-comma-separated-string-to-array-using-javascript
JavaScript - Convert Comma Separated String To Array - GeeksforGeeks
July 11, 2025 - The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified character, such as a comma.
Discussions

Convert comma separated string to a JavaScript array - Stack Overflow
I have this string: "'California',51.2154,-95.2135464,'data'" I want to convert it into a JavaScript array like this: var data = ['California',51.2154,-95.2135464,'data']; How do I do this? I d... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to convert string separated by commas to array? - Stack Overflow
Possible Duplicate: Convert JS object to JSON string Store comma separate values into array I have a string containing values separated with commas: "1,4,5,11,58,96" How could I turn it int... More on stackoverflow.com
🌐 stackoverflow.com
[javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
Trim after the split with map, so str.split(",").map((s) => s.trim()) Much easier to read than regex imo More on reddit.com
🌐 r/webdev
7
1
September 14, 2021
How to convert a comma-separated string into JS array?
let array = 'string'.split(','); Edit: MDN Documentation More on reddit.com
🌐 r/learnjavascript
1
2
August 4, 2020
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-convert-comma-separated-string-into-an-array-in-javascript.php
How to Convert Comma Separated String into an Array in JavaScript
You can use the JavaScript split() method to split a string using a specific separator such as comma (,), space, etc. If separator is an empty string, the string is converted to an array of characters.
🌐
W3docs
w3docs.com › javascript
How to Convert a Comma-Separated String into Array
Use a comma separator in the first argument of the split method if you want to split it by comma: ... Use the limit parameter to split a string into an array and also get the individual name.
🌐
GeeksforGeeks
geeksforgeeks.org › convert-comma-separated-string-to-array-using-javascript
JavaScript – Convert Comma Separated String To Array | GeeksforGeeks
November 26, 2024 - The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified character, such as a comma.
🌐
StackHowTo
stackhowto.com › home › web development › javascript › how to convert comma separated string into an array in javascript
How to Convert Comma Separated String into an Array In JavaScript - StackHowTo
October 12, 2021 - Youou can simply utilize the split() method of JavaScript to split a string using a particular separator, such as comma (,), space, etc. If the separator is an empty string, it is converted to an array of characters.
Find elsewhere
🌐
Designcise
designcise.com › web › tutorial › how-to-convert-a-comma-separated-string-to-an-array-in-javascript
How to Convert a Comma-Separated String to an Array in JavaScript? - Designcise
September 16, 2022 - Convert Comma-Separated String With Unknown Whitespaces Into an Array. You can convert a JavaScript comma-separated string (which has no spaces) into an array by using the String.prototype.split() method, for example, like so:
🌐
SourceFreeze
sourcefreeze.com › home › how to convert comma separated strings to array in javascript
How to convert comma separated strings to array in JavaScript - Source Freeze
December 15, 2023 - In this instance, split(‘,’) takes the comma as the separator and divides the string at each occurrence of the comma. This generates an array [‘apple’, ‘banana’, ‘orange’], allowing easy access to each fruit element.
🌐
GeeksforGeeks
geeksforgeeks.org › create-a-comma-separated-list-from-an-array-in-javascript
Create a Comma Separated List from an Array in JavaScript | GeeksforGeeks
December 28, 2024 - Using the split() Method (Most Common)The split() method is the simplest and most commonly used way to convert a comma-separated string into an array. It splits a string into an array based on a specified cha
🌐
Reddit
reddit.com › r/webdev › [javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
r/webdev on Reddit: [javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
September 14, 2021 -

i think this can be done by regEx by i don't how to look it up.

this is a learning project, but i am just thinking about the scale

i have a form where the user enter a bunch of categories and i want the user to separate those categories with a comma, but working on the case where the user add the comma but also a space after the comma (as we all do) or before the comma, how to go about treating this case, because i don't want to end up with two or three categories that are the same.

edit: i did it but i don't want to remove the post to help anyone with the same issue.

here's what i did

const categoriesAsString = e.target.value;

const categoriesTrimmed = categoriesAsString.trim();

const categoriesAsStringWithWhiteSpace = categoriesTrimmed.replace(/\s*,\s*/g,",");

const categoriesAsArray = categoriesAsStringWithWhiteSpace.split(",");

setCategories(categoriesAsArray);

🌐
Reddit
reddit.com › r/learnjavascript › how to convert a comma-separated string into js array?
r/learnjavascript on Reddit: How to convert a comma-separated string into JS array?
August 4, 2020 - let array = 'string'.split(','); Edit: MDN Documentation · Some useful JavaScript projects · r/learnjavascript • · upvotes · · comments · Most intuitive way to learn JS · r/learnjavascript • · upvotes · · comments · JavaScript has me soooo confused and it’s not even funny anymore ·
Top answer
1 of 3
1
One would 1st call the string's · split · method: · var list = ' item 1, item 2 , ,item 3 '; · var array = list · .split(','); · gs.debug('\n[' + array.join(']\n[') + ']'); · This prints: · *** Script: [DEBUG] · [ item 1] · [ item 2 ] · [ ] · [item 3 ] · Pretty all over the place. · For better result - where the leading and trailing spaces are removed - one would use · trim · before splitting: · var list = ' item 1, item 2 , ,item 3 '; · var array = list · .trim() · .split(','); · gs.debug('\n[' + array.join(']\n[') + ']'); · *** Script: [DEBUG] · [item 1] · [ item 2 ] · [ ] · [item 3] · Really only slightly better - only the 1st and the last items look good - and half of both only by chance (the one who entered the list did not add extra spaces after the last item and before the 1st one). · To take care of all extra spaces for all items that could exist due to sloppy fellow programmers composing the list or faulty data entry, one would switch the splitter to · RegExp · : · var list = ' item 1, item 2 , ,item 3 '; · var array = list · .trim() · .split(/\s*,\s*/g); · gs.debug('\n[' + array.join(']\n[') + ']'); · *** Script: [DEBUG] · [item 1] · [item 2] · [] · [item 3] · A lot better, almost there, just one problem remains: the 3rd item which is empty. · To take care of that problem one might · filter · the resulting array: · var list = ' item 1, item 2 , ,item 3 '; · var array = list · .trim() · .split(/\s*,\s*/g) · .filter(retainNotEmpty); · function retainNotEmpty (item) { · return '' != item; · } · gs.debug('\n[' + array.join(']\n[') + ']');​ · *** Script: [DEBUG] · [item 1] · [item 2] · [item 3] · Just about what one desires. · Filtering will also fix the issue of empty string ending up (not in a 0 length array, but) in an array with one item when split.
2 of 3
0
Hi @hardikbendre , · Please use the below to convert comma separated value into an array: · // dec stores the comma separated values · var dec = "service,now,community" · var colSplit = dec.split(","); · var arr=[]; · for(i=0;i
🌐
codestudy
codestudy.net › blog › how-can-i-convert-a-comma-separated-string-to-an-array
How to Convert a Comma-Separated String to an Array in JavaScript Using split() — codestudy.net
The split() method is JavaScript’s built-in solution for this, and it’s both powerful and flexible. In this blog, we’ll explore how to use split() to convert comma-separated strings to arrays, including handling edge cases like whitespace, ...