How to Convert JavaScript String to Array

To convert String to Array in JavaScript, use the split() method. The split() is a built-in JavaScript function that splits any string into an array of strings by separating it into two or more strings or substrings, using the data requested in the delimiter and limit parameters.

Syntax

split (delimiter, limit)

Arguments

delimiter: It is the value input by the user which specifies from which alphabet or any special symbol like “. ,; @ ? &” the String is to be split.

limit: This specifies up to how many delimiters are to be counted. Like till which point a string is to be converted into an array.

Return Value

The array.split() function returns an array containing the split values.

Code to convert String to Array in JavaScript

let data = "all.files.are.in.blue.folder";
const arr = data.split(".")
console.log(arr);

let x = "all.files.are.in.blue.folder";
const array = x.split(".", 3)
console.log(array);

Output

[ 'all', 'files', 'are', 'in', 'blue', 'folder' ]
[ 'all', 'files', 'are' ]

You can see that the split() function has converted an all.files.are.in.blue.folder” string into an array of length 6. Here the delimiter used is “.” And hence the String is split into arrays after each “.”.  

In the second example, we can see that we have added the limit in the split function parameter or argument; this precisely counts 3 delimiters which are “.” in this case, and hence only 3 values are printed in the Array.

We can also split a string by splitting from the white spaces it contains or by any specific alphabet.

How do the split() function work in JavaScript

The split() function checks the delimiter specified by the user and checks till the end of the String until a limit has been specified. Now you must be wondering what if a user does not input any delimiter in the String? The answer to that is that each character of the String is split into a value of an array.

Example

let k = 'HelloWorld';
const strsplit = k.split('');
console.log(strsplit);

Output

[
'H', 'e', 'l', 'l',
'o', 'W', 'o', 'r',
'l', 'd'
]

Converting String to an array using ArrayFrom function

The Array.from() is a built-in JavaScript static method that creates a new, shallow-copied Array instance from an array-like or iterable object. To convert a string to an array, use the Array.from() function.

Syntax

Array.form (arrayLike);

Arguments

The arrayLike argument specifies the String that we want to convert into an array.

Example

console.log(Array.from("JavaScript"));

Output

[
'J', 'a', 'v', 'a',
'S', 'c', 'r', 'i',
'p', 't'
]

Converting String to an array using the spread operator

The spread is a built-in JavaScript operator that can be used when all elements from an object or Array need to be included in a list of some kind. The Spread syntax can convert any string into an array, but it can also convert UTF-8 emojis into an array value.

Syntax

[…string];

Example

Let’s convert string to an array using the spread() operator.

let st = "hello😋"
console.log([...st]);

Output

[ 'h', 'e', 'l', 'l', 'o', '😋' ]

Conclusion

With the help of this easy syntax and examples, I hope that you would have understood how to convert a string to an array in JavaScript and how we can use delimit and limit parameters in the split() while also exploring different methods to like Array.from() and spread operator.

We already covered how to convert an array to string in JavaScript.

That’s it for this tutorial.

Leave a Comment