How to Convert a String to Array in JavaScript

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.


split (delimiter, limit)


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.


let data = "";
const arr = data.split(".")

let x = "";
const array = x.split(".", 3)


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

You can see that the split() function has converted an” 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('');



  'h', 'o', 'm', 'e',
  'r', ' ', 's', 'i',
  'm', 'p', 's', 'o',

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.

Leave a Comment