JavaScript String split() to Create Arrays

split() is a String method that divides a string into substrings and returns those pieces in a new array. You choose the separator, such as a space, comma, @, or regular expression.

const text = "this is my new tutorial";
const words = text.split(" ");
console.log(words); // ["this", "is", "my", "new", "tutorial"]

This page belongs in the array section because split() is one of the most common ways to create an array from text.

JavaScript split method converting a string into array elements

JavaScript String split() Syntax Top ↑

string.split(separator)
string.split(separator, limit)

The returned value is a new array. The original string is unchanged. The separator can be a string or a regular expression, and the optional limit restricts the maximum number of array entries returned.

Split a Sentence into an Array of Words Top ↑

This modernizes the original page's main example:

const text = "this is my new tutorial";
const words = text.split(" ");

for (const word of words) {
  console.log(word);
}
this
is
my
new
tutorial

The original example used join() with line breaks to display the array. The array itself is already created by split(); display formatting is a separate step.

Omitted Separator vs an Empty-String Separator Top ↑

These two cases are different and are easy to confuse.

const text = "Welcome to plus2net";

console.log(text.split());   // ["Welcome to plus2net"]
console.log(text.split("")); // array of UTF-16 code units

If the separator is omitted (or undefined), the entire string becomes one array element. An empty string "" splits at each UTF-16 code-unit boundary.

Limit the Number of Returned Array Elements Top ↑

The original page used a limit of 3. The behavior remains useful:

const text = "Welcome to plus2net. You can learn web programming";
const words = text.split(" ", 3);
console.log(words); // ["Welcome", "to", "plus2net."]

If limit is 0, the result is an empty array.

console.log("a,b,c".split(",", 0)); // []

Split an Email Address at @ Top ↑

The original page separated the local and domain portions of a simple email string:

const email = "username@example.com";
const parts = email.split("@");

console.log(parts[0]); // username
console.log(parts[1]); // example.com

This is fine for already-validated simple input. split("@") by itself is not a complete email-address validator.

Split Text with a Regular Expression Top ↑

A regular expression is useful when separators vary. For example, one or more whitespace characters:

const text = "JavaScript   PHP	Python";
const languages = text.trim().split(/\s+/);
console.log(languages); // ["JavaScript", "PHP", "Python"]

If a regular expression contains capturing groups, captured separator text can also appear in the resulting array, so use capturing groups intentionally.

Destructure Values Returned by split() Top ↑

const record = "Ravi|Hyderabad|Developer";
const [name, city, role] = record.split("|");

console.log(name); // Ravi
console.log(city); // Hyderabad
console.log(role); // Developer

Destructuring is convenient when the input format is known and has a fixed positional structure.

Using split() and join() Together Top ↑

split() converts a string into an array; join() combines array elements into a string.

const slug = "javascript-array-split";
const words = slug.split("-");
const title = words.join(" ");
console.log(title); // javascript array split

They are often complementary, but they are not perfect inverses for every separator and data set.

Splitting Characters, Unicode and Emoji Top ↑

split("") operates on UTF-16 code units and can split some Unicode characters incorrectly. Array.from() or spread syntax handles Unicode code points better.

const emoji = "A😄B";
console.log(Array.from(emoji)); // ["A", "😄", "B"]

Even code-point iteration does not automatically handle every user-perceived grapheme cluster, such as some combined emoji sequences.

Why split(", ") Is Not a Full CSV Parser Top ↑

For simple comma-separated values without quoting rules, split(",") is fine. Real CSV can contain quoted commas, escaped quotes, and line breaks, so use a CSV parser for general CSV input.

const simple = "red,green,blue";
console.log(simple.split(",")); // ["red", "green", "blue"]

Creating Arrays with split(): Video Top ↑

Array Creation Demo

Common split() Mistakes Top ↑

  • Thinking split() is an Array method; it is a String method that returns an array.
  • Confusing an omitted separator with an empty-string separator.
  • Using split("") for arbitrary Unicode text without considering surrogate pairs or grapheme clusters.
  • Expecting limit to return the unsplit remainder as the final item; extra text is simply not included once the limit is reached.
  • Using a simple comma split for general CSV data.
  • Using split("@") as complete email validation.

Array Reference Display Array Elements




Subscribe to our YouTube Channel here



plus2net.com







max

24-10-2012

Using a for loop (not a for-in loop.) Output 0 to 9 comma delineated no spaces. Output will be 0,1,2,3,4,5,6,7,8,9, in javascript



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer