0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert String to Array Easily

Use the split() method to convert a string to an array in JavaScript, for example: const arr = str.split(''); splits the string into an array of characters.
📋

Examples

Input"hello"
Output["h", "e", "l", "l", "o"]
Input"apple,banana,orange"
Output["apple", "banana", "orange"]
Input""
Output[]
🧠

How to Think About It

To convert a string to an array, think about how you want to split the string: by each character or by a specific separator like a comma. The split() method breaks the string into parts based on the separator you give it, returning an array of those parts.
📐

Algorithm

1
Get the input string.
2
Choose the separator to split the string (empty string for characters, or a specific character like comma).
3
Use the split method with the chosen separator on the string.
4
Return the resulting array.
💻

Code

javascript
const str1 = "hello";
const arr1 = str1.split('');
console.log(arr1);

const str2 = "apple,banana,orange";
const arr2 = str2.split(',');
console.log(arr2);
Output
[ 'h', 'e', 'l', 'l', 'o' ] [ 'apple', 'banana', 'orange' ]
🔍

Dry Run

Let's trace converting "apple,banana,orange" to an array using split(',')

1

Input string

"apple,banana,orange"

2

Split by comma

Call split(',') on the string

3

Resulting array

["apple", "banana", "orange"]

IterationCurrent Part
1apple
2banana
3orange
💡

Why This Works

Step 1: Using split() method

The split() method divides the string into pieces wherever it finds the separator you give it.

Step 2: Separator choice

If you use an empty string '' as separator, it splits into single characters; if you use a comma ',', it splits at commas.

Step 3: Returns an array

The method returns a new array containing all the parts after splitting.

🔄

Alternative Approaches

Array.from()
javascript
const str = "hello";
const arr = Array.from(str);
console.log(arr);
Creates an array from the string characters; simple and readable for character arrays.
Spread operator
javascript
const str = "hello";
const arr = [...str];
console.log(arr);
Uses spread syntax to unpack string characters into an array; concise and modern.

Complexity: O(n) time, O(n) space

Time Complexity

The split() method scans the entire string once, so time grows linearly with string length.

Space Complexity

It creates a new array holding all parts, so space used is proportional to the number of parts.

Which Approach is Fastest?

split(''), Array.from(), and spread operator all run in linear time; split() is best for splitting by separators, others are simpler for characters.

ApproachTimeSpaceBest For
split(separator)O(n)O(n)Splitting by any separator
Array.from()O(n)O(n)Converting string to array of characters
Spread operatorO(n)O(n)Converting string to array of characters
💡
Use split('') to get characters or split(',') to split by commas.
⚠️
Forgetting to provide a separator to split() results in the whole string as one array element.