Array to String in JavaScript
Initially, JavaScript was used only to make web interfaces, but nowadays, JavaScript is one of the most popular languages used in multiple arenas. Array and String are two of JavaScript's most widely used data containers.
The array in JavaScript holds multiple elements in a single variable. The string is used to hold the series of characters. We often encounter a situation where we need to convert an array to a string in JavaScript. There is a method in JavaScript known as toString(), which is used for this purpose.
Converting an Array to String in Javascript (Using toString() Method)
In order to convert an array into a string in Javascript, we simply apply the toString() method on the given array, and we get a stringified version of our array. Internally javascript first converts each element into string and then concretes them to return the final string.
Let's look into some widely used conversions.
Converting an Array of Numbers into a String in Javascript
The code for converting the array of numbers to string will be as follows :
Output :
As you can notice in the output that the square brackets are replaced by quotation marks. This indicates that the given array is converted into a string.
Converting an Array of Strings into Strings in Javascript
The code for converting the array of strings to strings will be as follows :
Output :
Converting Mix Arrays (both numbers and string) into Strings in Javascript
The code to convert array to string javascript is as follows :
Output :
Converting String Back to the Array In Javascript
There is no direct method present to do so. But if we take a closer look at the given scenario, then we will notice that the string contains all the elements of the array, and if we simply combine all the given elements into a single array, then we can get our array back.
Do you know any method in javascript that can be used in this case?
There is a method in javascript known as split() which splits a string into an array of substrings. The parameter that we pass in the split function is used to split the string. Thus if we pass ',' as a parameter to our split method, then we will get our required array.
Now let's look the code for above scenario :
Output :
As you can see integer values are also converted into the string. This is how the split method works, it cannot detect the previous type of string.
Working with Nested Arrays
There might be a situation when we have an array inside an array; such arrays are known as nested arrays. How to convert such an array into a string? toString() flattens the array. Thus all the elements of the array, including the nested array elements, get combined into a single array. And that's how we get the new array having all the elements separated by a comma.
Let's look into the code for this:
Output :
In the above example, the toString method flattens the array, and all the elements of the array, including the nested array elements, get combined into a single array.
Elevate your coding skills with our JavaScript course. Enroll today and become a proficient JavaScript developer!
Conclusion
- We can convert an array of numbers and an array of strings into strings using toString().
- We can also convert the nested array into string using toString().
- And in order to convert the string back to the array, we will use the split() method.