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:
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:
Lastly, display the output on the console with the help of “console.log()” method:
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:
Lastly, display the array on the console by using the “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:
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 array = text.split(' ');
Then, utilize the following code snippet to display the output on the console:
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 array= text.split('Linuxhint');
Lastly, display the output with the help of this command:
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 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.