Conversion of array into a string in JavaScript.
There are two method to convert element of array into a string in JavaScript.
1st Method: toString()-> toString() method joins the array & returns one string that contain each element of array.
2nd Method: Join()-> join() method joins all elements of an array into a string.
Example of toString()
var Fruit = ['Apple', 'Orange', 'Banana', 'Grape'];
var FruitString = Fruit.toString(); // assigns 'Apple,Orange,Banana,Grape' to FruitString
Example of join()
With the help of join method, we can join array in 4 different ways by using comma, space, plus and an empty string..
var Fruit = ['Apple', 'Orange', 'Banana', 'Grape'];
var FruitString = Fruit.join(); // assigns 'Apple,Orange,Banana,Grape' to FruitString
var FruitString = Fruit.join(', '); // assigns 'Apple, Orange, Banana, Grape' to FruitString
var FruitString = Fruit.join(' + '); // assigns 'Apple + Orange + Banana + Grape' to FruitString
var FruitString = Fruit.join(''); // assigns 'AppleOrangeBananaGrape' to FruitString
0 Comment(s)