To convert a string to an array in JavaScript, call the string's split() method and pass the separator you want to split on: "a,b,c".split(",") returns ["a", "b", "c"]. With no argument, split() returns the whole string as a single-element array; with an empty string "", it splits into individual characters. That is the shortest working answer — the rest of this guide covers the options, edge cases and fixes.
What You'll Need
- A JavaScript environment: a browser's developer console, Node.js, or any editor that can run JS.
- A string value to work with — a literal like
"red,green,blue"or a variable holding text. - Basic familiarity with arrays and how to read a console output.
Step-by-Step
- Identify the separator. Look at your string and decide what character or sequence divides the pieces you want. Commas, spaces, hyphens, newlines and pipes are common. The separator is not included in the resulting array elements.
- Call
split()with that separator. For example:const parts = "red,green,blue".split(",");This produces["red", "green", "blue"]. - Check the output. Log it with
console.log(parts)or inspect it in your browser's developer console. Confirm the number of elements and that no separator characters remain inside them. - Handle whitespace. If your string is
"red, green, blue", splitting on","leaves a leading space on each element after the first. Either split on", "(comma plus space) or trim each element afterwards withparts.map(s => s.trim()). - Split on multiple possible separators.
split()accepts a regular expression. To split on commas, semicolons or pipes, usetext.split(/[,;|]/). To split on any run of whitespace, usetext.split(/\s+/). - Limit the number of results if needed. Pass a second argument:
"a,b,c,d".split(",", 2)returns["a", "b"]. The rest of the string is discarded, so use this only when you deliberately want a cap. - Split into characters. Use an empty string:
"hello".split("")gives["h", "e", "l", "l", "o"]. For text containing emoji or other characters outside the basic range, useArray.from("hello")or the spread operator[..."hello"]instead, becausesplit("")can break surrogate pairs. - Split on line breaks. For text pasted from a textarea or file, use
text.split(/\r?\n/)to handle both Windows and Unix line endings. Filter out blank lines with.filter(line => line.trim() !== "")if needed. - Store or use the array. Assign the result to a variable, pass it to another function, or iterate with
forEach,mapor afor...ofloop.
Common Mistakes
- Forgetting that
split()returns a new array. It does not modify the original string — strings are immutable. If you ignore the return value, nothing appears to happen. - Using the wrong separator. Splitting
"a-b-c"on","returns the whole string as one element because the comma never appears. - Leaving whitespace in elements. Splitting on a comma when the source uses comma-plus-space produces elements with leading spaces that break comparisons and lookups.
- Assuming
split("")is safe for all Unicode. It splits UTF-16 code units, so emoji and some accented characters can be torn apart. UseArray.from()or the spread operator for character-level splitting. - Confusing
split()withjoin().split()turns a string into an array;join()turns an array back into a string. - Passing a number as the separator. The separator is converted to a string, so
"a1b".split(1)splits on the character"1", which may not be what you intended.
Tips
- Use a regular expression when separators vary or repeat, such as
/\s+/for arbitrary whitespace. - Chain
.map(s => s.trim())after splitting user-supplied text to clean up stray spaces. - Filter empty strings with
.filter(Boolean)when the source may contain consecutive separators. - For CSV-like data, remember that
split(",")does not understand quoted fields containing commas. A proper CSV parser is needed for that;split()is fine only for simple, well-formed data. - Prefer
String.prototype.splitover manual loops — it is built in, readable and fast enough for typical text processing.
Troubleshooting
If split() is not giving the result you expect, work through these checks in order:
- Confirm the separator actually exists. Log the raw string and look for hidden characters, tabs or non-breaking spaces. Copy-pasted text often contains characters that look like spaces but are not.
- Check the type. If the value is not a string (for example, a number or
undefined),split()will throw. Convert first withString(value). - Inspect the array length. If it is 1, the separator was not found. If it is larger than expected, the separator appears more often than you thought or your regex is matching more broadly.
- Test the regex separately. Run your pattern against a sample string in the console to see what it matches before using it in
split(). - Watch for special regex characters. If you build a regex from a user-supplied separator, escape it — characters like
.,*,+,?,(and[have special meaning.
Frequently Asked Questions
Does split() change the original string?
No. Strings in JavaScript are immutable, so split() returns a new array and leaves the original string untouched.
How do I split a string into words?
Use a whitespace regex: text.split(/\s+/). This treats any run of spaces, tabs or newlines as a single separator.
Why does split("") break emoji?
It splits by UTF-16 code unit, and many emoji use two code units. Use Array.from(text) or [...text] to split by full characters instead.
Can I split on more than one separator at once?
Yes, with a character class in a regex, such as text.split(/[,;|]/), which splits on any of those characters.
What happens if the separator is not found?
You get an array with one element: the entire original string. Check the array length to detect this.
How do I turn the array back into a string?
Use array.join(separator), for example ["a", "b"].join(",") returns "a,b".