Mateen Kiani
Published on Sun Jul 06 2025·2 min read
Converting a string into an array is a common task in JavaScript. Whether you’re processing CSV data or breaking down user input character by character, arrays let you manipulate and iterate over each element with ease.
In this article, we’ll explore several ways to turn strings into arrays, from the built-in split method to more advanced techniques like Array.from and JSON.parse. You’ll learn tips to choose the right approach based on your data format.
The simplest way to break a string into parts is using String.prototype.split
. Pass a delimiter, and split returns an array of substrings:
const csv = "apple,banana,cherry";const fruits = csv.split(",");console.log(fruits);// ["apple", "banana", "cherry"]
This method is perfect for:
Once you have an array, you can iterate with methods like forEach.
Tip: If your delimiter has special regex characters (like
.
or*
), escape them or pass a string instead of a regex.
If you want each character as an array element, use Array.from
or the spread operator:
const word = "hello";const chars1 = Array.from(word);const chars2 = [...word];console.log(chars1);// ["h", "e", "l", "l", "o"]console.log(chars2);// ["h", "e", "l", "l", "o"]
Choose this when you need to:
map
or filter
on the characters.Benefit: Both methods handle Unicode correctly, splitting characters rather than code units.
When your string is a JSON array (like '["a", "b", 1]'
), use JSON.parse:
const jsonString = '["JavaScript", "Python", "Ruby"]';const languages = JSON.parse(jsonString);console.log(languages);// ["JavaScript", "Python", "Ruby"]
This approach is safe for:
Note: Invalid JSON will throw an error. Wrap in
try/catch
to handle it gracefully.
For custom logic or performance tuning, loop through the string:
const input = "12345";const result = [];for (let i = 0; i < input.length; i++) {result.push(input[i]);}console.log(result);// ["1", "2", "3", "4", "5"]
Use loops when you need to:
Now you know multiple ways to convert strings into arrays in JavaScript. From quick splits to custom loops and JSON parsing, pick the method that fits your data format and performance needs. Give these techniques a try in your next project and simplify your string handling code.