JavaScript

How to Convert a String Into an Array in JavaScript

In high-level languages, strings are the most productive fast, and efficient data structures. Whereas the array consists of similar string data elements. Moreover, arrays can be utilized for storing the different values in a single variable. Every array element has a unique number linked with it, which is called a numeric index, that permits the users for accessing it. Arrays in JavaScript are usually begin with index zero and can be manipulated with different methods.

This post will demonstrate converting the string into an array in JavaScript.

How to Convert/Modify a String Into an Array in JavaScript?

To convert/modify a string into an array in JavaScript, multiple methods can be utilized. Some of them are listed below:

Method 1: Converting a String Into an Array Using Array.from() Method

To modify a string in an array a JavaScript “Array.from()” method can be used. When dealing with strings, each letter of the string is transformed into a member of the new array instance, but when dealing with integer values, the new array instance simply takes the elements of the existing array.

Syntax

To utilize the Array.from(), the following method can be used:

Array.from(object, mapFunction, thisValue)

Now, execute the following instructions:

  • First, declare a variable and assign a value to that variable. To do so, a variable with the name “myname” is declared.
  • Next, take another variable with a different name, and use the “Array.from()” method to convert the string value individually:
let myname = "Hafsa Javed";

let nameChars = Array.from(myname);

Lastly, display the output on the console with the help of “console.log()” method:

console.log(nameChars);

It can be seen that the string has been converted onto an array. Each index of it contains a single character, respectively:

Method 2: Converting a String Into an Array Using Object.assign() Method

JavaScript “Object.assign()” method can be utilized for splitting the string into separate characters or values. To do so, follow the stated instructions listed below:

  • Initialize the variable and assign a value according to your preference.
  • Then, declare another variable with a different name and use the “Object.assign()” method to transform the string into individual characters:
let Fname = "Linuxhint";

let nameChar = Object.assign([], Fname);

Lastly, display the array on the console by using the “console.log(nameChar)”:

console.log(nameChar);

Method 3: Converting a String Into an Array Using split() Method

The “split()” method of JavaScript is used for converting the string into a substring in an array. The blank space is utilized as an operator to change the string into a substring in an array. Moreover, the split() method cannot modify the original/actual string.

Syntax

To utilize the split() method, use the below stated syntax:

string.split(separator, limit)

Here:

  • separator” is used to add space between string words and convert it into substring.
  • limit” is used for specifying the limit of the string.

Example 1: Split String by Adding Space and Store it in Array

The “split()” method is invoked for splitting the string into individual words. Here, the “’ ‘” is used as a separator to add space between the word in the string:

let text= 'This is Linuxhint Website';

let array = text.split(' ');

Then, utilize the following code snippet to display the output on the console:

console.log(array)

Example 2: Split Text by Removing one Substring and Store it in an Array

In this example, the “Linuxhint” parameter is set as a string separator to split the specified string into different substring:

let text= 'This is Linuxhint Website';

let array= text.split('Linuxhint');

Lastly, display the output with the help of this command:

console.log(array);

Output

Method 4: Converting a String Into an Array Using spread[…] Operator

The spread[…..] operator can also be utilized to convert a string into an array. To do so, initialize the variable and set the value. Then, utilize the “[…name]” as a value of other variable that will spread the string into an array as an individual character:

let name = "Linuxhint";

let nameChar = [...name];

console.log(nameChar);

Output

That’s all about converting the string into an array by utilizing different methods.

Conclusion

To convert a string into an array in JavaScript, there are multiple methods that can be utilized, such as “Array.from()”, “Object.assign()”, “split()” methods, and “spread[…]” operator. Where “Array.from()” is used to convert the string into a substring. This write-up has stated the different methods for converting the string into an array in JavaScript.

About the author

Hafsa Javed