
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
268
|
Chapter 11: Arrays
series of characters) called a delimiter. Finally, join() returns the resulting string. The
syntax of join() is:
arrayName.join(delimiter)
where delimiter is the string used to separate the converted elements of arrayName.
If
delimiter is unspecified, it defaults to a comma. The result of a join() statement is
most easily understood through an example:
var siteSections = ["animation", "short films", "games"];
// Sets siteTitle to "animation>> short films>> games"
var siteTitle = siteSections.join(">> ");
// Sets siteTitle to "animation:short films:games"
var siteTitle = siteSections.join(":");
Note that join() does not modify the array upon which it is invoked. Instead, it
returns a string based on that array.
When called without a delimiter argument, join() behaves exactly like toString().
Because toString() does not add a space after the comma it uses to delimit elements,
we may wish to use join() with “, ” as the delimiter if we want nicely formatted out-
put:
var x = [1, 2, 3];
trace(x.join(", ")); // Displays: "1, 2, 3" instead of "1,2,3"
The join() method has a potentially surprising result when used on an array contain-
ing elements that are themselves arrays. Since join() converts elements to strings, and
arrays are converted to strings via their toString()