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

[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
Convert a String containing commas as separators into an Array
I have a string of text “01pdf1,01sb1”. The comma in the text is a separator between two elements: “01pdf1” and “01sb1”. I want to conduct a series of operations for each element. In order to do this, I think I should use the iterator module. In order to use the iterator, I need ... More on community.make.com
🌐 community.make.com
3
0
January 31, 2023
Convert comma separated string to a JavaScript array - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I don't have jQuery. And I don't want to use jQuery. ... You can iterate over the values and process them accordingly... but I see your point. It would have helped if you posted what you have tried so far an explicitly pointed out what you have problems with. If you just ask how to convert this comma separated string into this array... More on stackoverflow.com
🌐 stackoverflow.com
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
Read this tutorial and learn information about the JavaScript built-in split() method which is called for converting a comma-separated string in an array.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-comma-separated-string-to-array
Convert a comma-separated String to an Array in JavaScript | bobbyhadz
Use the String.split() method to convert a comma-separated string to an array. The split() method will split the string on each occurrence of a comma and will return an array containing the results.
🌐
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);

Find elsewhere
🌐
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 a versatile tool for converting comma-separated strings to arrays in JavaScript. By mastering its parameters (separator and limit) and handling edge cases like whitespace or empty values, you can efficiently parse and ...
🌐
Make Community
community.make.com › questions
Convert a String containing commas as separators into an Array - Questions - Make Community
January 31, 2023 - I have a string of text “01pdf1,01sb1”. The comma in the text is a separator between two elements: “01pdf1” and “01sb1”. I want to conduct a series of operations for each element. In order to do this, I think I should use the iterator module. In order to use the iterator, I need ...
🌐
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
🌐
OneCompiler
onecompiler.com › javascript › 3uaemw5yq
Convert Javascript string array comma separated string
let arrayName = [value1, value2,..etc]; // or let arrayName = new Array("value1","value2",..etc);
🌐
Codemia
codemia.io › home › knowledge hub › how can i convert a comma-separated string to an array?
How can I convert a comma-separated string to an array? | Codemia
January 8, 2025 - A CSV line can contain quoted commas, escaped quotes, and embedded newlines. For example, "New York, NY" is one field, not two. If the input is real CSV, use a CSV parser instead of plain string splitting. ... 1import csv 2from io import StringIO 3 4text = 'apple,"New York, NY",orange' 5row = next(csv.reader(StringIO(text))) 6print(row) JavaScript in Node.js usually relies on a CSV library for this case.