How to convert string to array in JavaScript


In programming, converting a string into an array is a common practice, and JavaScript provides us with different in-built function to do this task very easily.

In JavaScript, the most common method to do this task is to use the split() method.

However, in this article, we will see three different ways to convert a string to an array using three different methods:

  1. split()
  2. Spread Operator
  3. Array.from()

So, let's see each with an example.

Method 1: Converting string to an array using split() method

The split() method in JavaScript is used to divide a string into an array of substrings. This method does not change the original string, instead, it returns the new array.

Syntax:

string.split(separator, limit)

seperator : It is a single character that is used to split a delimited string. It can be a simple string or a regular expression.

limit : It is optional. It is used to limit the number of splits of the string. Any item after the limit is excluded.

Example:

let str = "who let the dogs out";

const arr = str.split(" ");
console.log(arr)

Output:

[ 'who', 'let', 'the', 'dogs', 'out' ]

In the above code, we have passed space " " as a separator and so the string is split by each space and converted into an array.

Now, let's see what happens if we leave the separator empty.

let str = "who let the dogs out";

const arr = str.split("");
console.log(arr)

Output:

[
  'w', 'h', 'o', ' ', 'l', 'e', 't', ' ', 't', 'h',
  'e', ' ', 'd', 'o', 'g', 's', ' ', 'o', 'u', 't'
]

It splits the string and returns all the characters in the string as individual elements in the array.

Using limit in split()

Now let's see how to use the limit parameter in the split() method.

For example, we will set the limit to 3 .

let str = "who let the dogs out";

const arr = str.split(" ", 3);
console.log(arr) // [ 'who', 'let', 'the' ]

As you can see, it splits the string up to the third space and returns us the array.

Method 2: Using the Spread operator to convert string to array.

We can also use the ES6 spread operator ( ... ) as an alternative to split an array in JavaScript.

Example:

let str = "who let the dogs out";

const arr = [...str]
console.log(arr)

Output:

[
  'w', 'h', 'o', ' ', 'l', 'e', 't', ' ', 't', 'h',
  'e', ' ', 'd', 'o', 'g', 's', ' ', 'o', 'u', 't'
]

As you can see, the spread operator returns all the characters in the string as individual elements in the array.

It works if you do not want to divide the words of the string as different elements in the array.

It works better when you have a single word and you want to split it as different elements in an array. For example,

let str = "aeroplane";

const arr = [...str]
console.log(arr) // [ 'a', 'e', 'r', 'o', 'p', 'l', 'a', 'n', 'e' ]

Method 3 : Using Array.from() for converting string to array.

The Array.from() method makes a shallow-copied array instance from iterable or array-like objects in JavaScript.

Syntax:

Array.from(object)

object : Array-like or iterable objects to convert to an array.

Example:

let str = "aeroplane";
let arr = Array.from(str);
console.log(arr) // [ 'a', 'e', 'r', 'o', 'p', 'l', 'a', 'n', 'e' ]

That's it, these are the 3 ways that can be used to convert a given string to an array. And the best and easy one would be the split() method because it is much more convenient to use compared to the other two methods.


💾 Recommended Articles