You are currently viewing 3 Easy Ways to Turn String into Array in JavaScript

3 Easy Ways to Turn String into Array in JavaScript

Turn String into Array JavaScript

You can convert words into an array of characters, a sentence into an array of words, or you can convert a sentence into an array with a specified delimiter. With the help of the JavaScript split() method, you can do it easily. You can easily perform this operation if you know how to convert from array to string. So, In this article, we will see how to turn a string into an array of javascript with different strategies.

3 Ways to Convert String to Array

Method 1: Using the split() method

If you use the split() method you must pass an argument to it. You have to pass the separator in the first argument and pass your limit in the second argument. If you leave the second argument unpassed, it will automatically take the entire string. So pass the second argument only if you have any limitations. It’s stored string into an array of substrings depending on the (separator) you pass.

Example:

const str = "Learn, JavaScript Easily";
const arrChar = str.split("", 5);
const arrStr = str.split(" ");
const arrCom = str.split(",");

console.log(arrChar); //['L', 'e', 'a', 'r', 'n']
console.log(arrStr); //['Learn,', 'JavaScript', 'Easily']
console.log(arrCom); //'Learn', ' JavaScript Easily']
  1. In this example, we have created the variable "str" with string values. Then we use the split() method on the ‘str’ string by passing the "" empty separator as the first argument and 5 as the second argument. So, the string will be split into individual characters of the first 5 characters.
  2. To store each word in an array, use the split() method and pass the " " space separator as an argument.
  3. Finally, we split into an array of substrings whenever a comma is encountered by passing the (,) comma as a separator in the split() method.
READ ALSO  3 Easy Ways to Format the Number to 2 Decimals in JavaScript

Method 2: Using the spread operator (…)

The spread operator is the (three consecutive dots: …) and can also be used to convert a string into an array. It is one of the very simplest ways to convert a string into an array. We mostly use the spread operator in conjunction with some method or function. If we use the spread operator on a square bracket, it will automatically store the characters in the array of elements.

Example:

const str = 'JavaScript';
const arr = [...str];
console.log(arr);

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

You can make use of the match method along with a regular expression to split the string into words. Here’s an example:

Example:

const sentence = 'I love JavaScript';
const words = [...sentence.match(/\b\w+\b/g)];
console.log(words);

//Output: ['I', 'love', 'JavaScript']

In this above example, we have used the spread operator (…) with the match() method in square brackets. The match method is used with the regular expression /\b\w+\b/g, it checks all word characters. So, the match method will return an array of matched words. Finally, it converts each word from the original string as separate elements.

Method 3: Using the Array.prototype.map() method

We can also use the Array.prototype.map() built-in method to convert a string to an array of characters. We can invoke the “call()” method of Array.prototype.map() method and pass two arguments to it. The first argument is a string that we want to convert into an array of characters and in the second argument, we have to pass the callback function char => char. This “char => char” returns the character itself, unchanged. So, the resulting array contains each character of the string as separate elements.

READ ALSO  What is Javascript Array with Example | Advanced Guide

Example:

const str = 'JavaScript';
const arr = Array.prototype.map.call(str, char => char);
console.log(arr);

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

Conclusion

So, you can use these three methods to turn a string into an array in javascript.

Leave a Reply