Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use JavaScript’s built-in split() method. jQuery does not need a special string-to-array converter: split the string wherever its delimiter occurs, and JavaScript returns an array.

const values = input.split(",");

For comma-separated values that may have spaces around the commas, use a regular expression:

const values = input.split(/s*,s*/);

Split a string into an array

String.prototype.split() accepts a string or regular expression as its separator and returns an array of substrings. This is a JavaScript method, so it works in a page that uses jQuery without calling a jQuery conversion function. The jQuery Types documentation also demonstrates splitting a string: jQuery’s Types documentation.

const colors = "red,green,blue".split(",");

console.log(colors); // ["red", "green", "blue"]

Use the separator that matches the string’s actual format. If the delimiter is not present, the array contains the original string as its only element.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Handle spaces and form values

Ignore spaces around commas

When values may have whitespace before or after a comma, /s*,s*/ matches the comma and any surrounding whitespace:

const tags = "news, technology, reviews".split(/s*,s*/);

console.log(tags); // ["news", "technology", "reviews"]

This removes whitespace adjacent to each comma, but it does not trim whitespace at the very start or end of the entire input. If those spaces are possible, trim the whole string first:

Rank #2
Sale
JavaScript and jQuery: Interactive Front-End Web Development
  • JavaScript Jquery
  • Introduces core programming concepts in JavaScript and jQuery
  • Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
const tags = input.trim().split(/s*,s*/);

Read a value from a jQuery-selected form control

For a control with an ID of tags, call .val() to get its string value, then split that value. Guard against a missing or empty value if the code needs to handle those cases:

const raw = $("#tags").val();
const tags = typeof raw === "string" && raw !== ""
  ? raw.split(/s*,s*/)
  : [];

This example treats an empty field as an empty array. If preserving the empty field as a one-item result is important, split it instead; the right behavior depends on what the application considers a valid value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Split strings safely: separator and edge cases

  • No matching separator: "blue".split(",") returns ["blue"].
  • Separator at an edge: a separator at the start or end produces an empty string at that edge. For example, ",blue,".split(",") returns ["", "blue", ""].
  • Empty separator: "".split("") returns an empty array. A nonempty string split with "" is divided into UTF-16 code units, which may not correspond to user-perceived characters.
  • Separator omitted: calling split() with no separator returns an array containing the whole string.
  • Limit: the optional second argument limits the number of returned entries, as in "red,green,blue".split(",", 2), which returns ["red", "green"].

For details on how string and regular-expression separators behave, see MDN’s split() reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When the input is a jQuery collection, use .map()

A jQuery object is a wrapped collection of matched elements, not a native JavaScript Array. If the goal is to collect a property from DOM elements, use jQuery’s .map() and then .get() to obtain a plain array:

const ids = $("li").map(function () {
  return this.id;
}).get();

For example, this produces an array of the IDs of the matched list items. The jQuery Learning Center recommends .map() for building an array from matched elements and explains that .get() converts the wrapped result to a basic JavaScript array: jQuery’s iteration guide. The distinction is the input: use split() for one delimited string, and .map().get() for values gathered from a DOM collection. See also the jQuery() API documentation.

Quick Recap

SaleBestseller No. 1
Web Design with HTML, CSS, JavaScript and jQuery Set
Web Design with HTML, CSS, JavaScript and jQuery Set
Brand: Wiley; Set of 2 Volumes
$35.05
SaleBestseller No. 2
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript and jQuery: Interactive Front-End Web Development
JavaScript Jquery; Introduces core programming concepts in JavaScript and jQuery; Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
$22.80

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.