Availability: |
| ||||||||
Property/method value type: | String primitive | ||||||||
JavaScript syntax: | - | myArray.join(aSeparator) | |||||||
Argument list: | aSeparator | A string to place between array elements as the array is concatenated to form a string. |
The result of this method will be a String primitive containing the array elements interposed with separators.
The elements in the array are converted to strings and are concatenated together to form a larger string. Each element has the separator value placed between it and the next element.
If the separator is not specified, then a single comma is used to join the array elements. This means that if you want no separation between the joined items you should pass an empty string as the separator value.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> // Demonstrate array joins myString1 = "This is a sentence made of words."; document.write("Original input string<BR>") document.write(myString1) myArray = myString1.split(" "); document.write("<BR><BR>String split into an array<BR>") displayArrayAsTable(myArray); myString2 = myArray.join("+"); document.write("<BR><BR>Array joined up as a string<BR>") document.write(myString2) // Display an array in a table function displayArrayAsTable(anArray) { myLength = anArray.length; document.write("<TABLE BORDER=1>"); for(myIndex = 0; myIndex < myLength; myIndex++) { document.write("<TR><TD>"); document.write(myIndex); document.write("</TD><TD>"); document.write(anArray[myIndex]); document.write("</TD></TR>"); } document.write("</TABLE>") } </SCRIPT> </BODY> </HTML>
See also: | Array.prototype, Cast operator, String concatenate (+), String.split() |
Prev | Home | Next |
Array.input | Up | Array.length |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |