This article mainly introduces how to convert array elements into strings in JavaScript, which is the basic knowledge in JS learning, for more information, see slice ():
Source code:
Click the button to extract the second and the third elements from the array.
Try it《script》function myFunction(){var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];var citrus = fruits.slice(1,3);var x=document.getElementById("demo");x.innerHTML=citrus;}《script》
Test results:
Orange,Lemon
We can use array elements to form a string. the related join () method is used as an example:
Source code:
Click the button to join the array elements into a string.
Try it《script》function myFunction(){var fruits = ["Banana", "Orange", "Apple", "Mango"];var x=document.getElementById("demo");x.innerHTML=fruits.join();}《script》
Test results:
Banana,Orange,Apple,Mango
To directly convert an array to a string, you can use the toString () method:
Source code:
Click the button to convert the array into a string and return it.
Click meScript function myFunction () {var fruits = ["Banana", "Orange", "Apple", "Mango"]; var str = fruits. toString (); var x = document. getElementById ("demo"); x. innerHTML = str;} script
Test results:
Banana,Orange,Apple,Mango