To convert a string to an array in JavaScript, you can use the “split()” method. The split() is a built-in 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: The value input by the user specifies from which alphabet or any special symbol like “. ,; @ ? &” the String is to be split.
limit: This specifies 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.
Example
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.
The split() method is called on a string and returns an array. The separator argument can be any character or sequence of characters, including a space, a comma, a semicolon, a hyphen, or any other character.
If the separator argument is a space, the split() method splits the string into an array of single characters.
const mainString = "homer simpson";
const mainArray = mainString.split('');
console.log(mainArray);
Output
[
'h', 'o', 'm', 'e',
'r', ' ', 's', 'i',
'm', 'p', 's', 'o',
'n'
]
In this code example, the split() method is called on mainString with no separator argument. This splits the string into an array of single characters.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.