Last modified: Jul 29, 2026
JavaScript Convert String to Array
Converting a string to an array is a common task in JavaScript. You often need to break a string into smaller parts for processing. This guide shows you the best ways to do it.
We will cover four main methods. Each method works for different situations. You will learn when to use each one.
By the end, you will confidently convert any string into an array. Let's start with the most popular method.
1. Using the split() Method
The split() method is the most common way to convert a string to an array. It splits a string into an array of substrings based on a separator.
You provide a separator as an argument. The string is cut at each occurrence of that separator. The results become array elements.
Example: Split by comma
// Split a string by comma
let fruits = "apple,banana,orange";
let fruitArray = fruits.split(",");
console.log(fruitArray);
// Output: ["apple", "banana", "orange"]
If you use an empty string as the separator, you get an array of individual characters.
Example: Split every character
// Split into individual characters
let word = "hello";
let chars = word.split("");
console.log(chars);
// Output: ["h", "e", "l", "l", "o"]
The split() method also accepts a limit parameter. This restricts the number of items in the resulting array.
Example: Split with limit
// Limit the array to 3 items
let data = "a,b,c,d,e";
let limited = data.split(",", 3);
console.log(limited);
// Output: ["a", "b", "c"]
2. Using the Spread Operator (...)
The spread operator provides a modern and clean way to convert a string to an array. It expands the string into individual characters.
This method is perfect for splitting a string into an array of characters. It is concise and easy to read.
Example: Spread operator
// Convert string to array with spread operator
let greeting = "world";
let letters = [...greeting];
console.log(letters);
// Output: ["w", "o", "r", "l", "d"]
The spread operator works with any iterable, not just strings. It is a favorite among modern JavaScript developers.
3. Using Array.from()
The Array.from() method creates a new array from any iterable or array-like object. It works well for converting strings to arrays.
This method is similar to the spread operator. However, it also supports a mapping function as a second argument.
Example: Basic Array.from()
// Convert string to array using Array.from
let name = "code";
let nameArray = Array.from(name);
console.log(nameArray);
// Output: ["c", "o", "d", "e"]
Example: Array.from() with mapping
// Convert and transform each character
let text = "abc";
let uppercased = Array.from(text, char => char.toUpperCase());
console.log(uppercased);
// Output: ["A", "B", "C"]
The mapping function gives you extra power. You can modify each element as you create the array.
4. Using Object.assign()
The Object.assign() method can also convert a string to an array. It copies the string's characters into a new array.
This method is less common but still useful. It works by assigning the string's indexed characters to an array.
Example: Object.assign()
// Convert string to array using Object.assign
let str = "test";
let arr = Object.assign([], str);
console.log(arr);
// Output: ["t", "e", "s", "t"]
Note that this method creates a shallow copy of the string's characters. It is not as widely used as split() or the spread operator.
5. Handling Special Cases
Sometimes you need to handle strings with special characters. For example, strings with emojis or multi-byte characters.
The split() method with an empty string can break emojis. This is because emojis are made of two code units.
Example: Problem with emojis
// Emoji breaks with split('')
let emoji = "😀😎";
let broken = emoji.split("");
console.log(broken);
// Output: ["\ud83d", "\ude00", "\ud83d", "\ude0e"]
To handle emojis correctly, use Array.from() or the spread operator. They respect Unicode code points.
Example: Correct emoji handling
// Array.from handles emojis correctly
let emoji = "😀😎";
let correct = Array.from(emoji);
console.log(correct);
// Output: ["😀", "😎"]
6. Converting Between String and Array
Sometimes you need to go the other way. You might want to convert an array back to a string. This is useful for storing or displaying data.
If you need to reverse the process, check out our guide on JavaScript Array to String. It covers all the methods for joining array elements into a string.
Understanding both directions gives you full control over your data transformation.
7. Practical Examples
Let's look at some real-world use cases for converting strings to arrays.
Example: Parsing CSV data
// Parse a simple CSV line
let csvLine = "John,Doe,30,New York";
let fields = csvLine.split(",");
console.log(fields);
// Output: ["John", "Doe", "30", "New York"]
Example: Counting characters
// Count characters using array length
let sentence = "hello world";
let charArray = [...sentence];
console.log("Character count:", charArray.length);
// Output: Character count: 11
Example: Reversing a string
// Reverse a string using array methods
let original = "javascript";
let reversed = original.split("").reverse().join("");
console.log(reversed);
// Output: "tpircsavaj"
8. Performance Considerations
For most use cases, performance differences are minimal. However, if you work with very large strings, you should be aware of some trade-offs.
The split() method is generally the fastest for simple separators. The spread operator and Array.from() are slightly slower but more readable.
Choose the method that makes your code clear. Optimize only when you have proven performance issues.
9. Working with Array Methods
Once you have an array, you can use all the powerful JavaScript Array Methods Guide. Methods like map(), filter(), and reduce() become available.
For example, you can filter characters or transform them easily. This is why converting strings to arrays is so valuable.
You can also check the JavaScript Array Length Guide to understand how to work with array sizes.
10. Common Mistakes
Avoid these common mistakes when converting strings to arrays.
Mistake 1: Forgetting the separator
// Forgetting the separator returns the whole string as one element
let str = "hello";
let arr = str.split();
console.log(arr);
// Output: ["hello"]
Mistake 2: Using split('') with emojis
As shown earlier, this breaks multi-byte characters. Always use Array.from() for Unicode safety.
Mistake 3: Assuming split modifies the original string
The split() method does not modify the original string. It returns a new array. Strings in JavaScript are immutable.
Conclusion
Converting a string to an array in JavaScript is easy with the right method. Use split() for delimiter-based splitting. Use the spread operator or Array.from() for character arrays.
Each method has its strengths. Choose based on your specific needs. For Unicode safety, prefer Array.from() or the spread operator.
Remember to handle special cases like emojis carefully. Practice with examples to build your confidence.
Now you have the skills to convert any string to an array in JavaScript. Happy coding!