In JavaScript and Jquery how to convert the string to array and same array convert to string and check them using typeof method in JavaScript.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
Create an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.
JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.
From String to Array you can Use split()
Method
var str = "How are you doing today?";
var res = str.split(" ");
console.log(res); // How,are,you,doing,today? it will print
From Array to String you can use Join()
method or toString()
Method
var arr = "abcdef".split(''); // creates an array from a string
var str = arr.join(''); // creates a string from that above array
If you want do it manually, without any JavaScript methods. Try the below
String to Array
var str = "STRING";
var arr = [];
for(int i=0; i<=str.length; i++)
arr[i] = str.charAt(i);
Array to String
var str = "";
var arr = ["S","T","R","I","G"];
for(int i=0; i<=arr.length; i++)
str +=arr.charAt(i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With