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.
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.
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.
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.
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)); // []
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.
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.
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.
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.
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.
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"]
split() is an Array method; it is a String method that returns an array.split("") for arbitrary Unicode text without considering surrogate pairs or grapheme clusters.limit to return the unsplit remainder as the final item; extra text is simply not included once the limit is reached.split("@") as complete email validation.Array Reference Display Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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 | |