Array Methods
ECMAScript 3 defines a number of useful array manipulation
functions on Array.prototype, which means that they are
available as methods of any array. These ECMAScript
3 methods are introduced in the subsections below. As usual,
complete details can be found under Array in the core reference section.
ECMAScript 5 adds new array iteration methods; those methods are
covered in ECMAScript 5 Array Methods.
join()
The Array.join() method
converts all the elements of an array to strings and concatenates
them, returning the resulting string. You can specify an optional
string that separates the elements in the resulting string. If no
separator string is specified, a comma is used. For example, the
following lines of code produce the string “1,2,3”:
vara=[1,2,3];// Create a new array with these three elementsa.join();// => "1,2,3"a.join(" ");// => "1 2 3"a.join("");// => "123"varb=newArray(10);// An array of length 10 with no elementsb.join('-')// => '---------': a string of 9 hyphens
The Array.join() method is
the inverse of the String.split()
method, which creates an array by breaking a string into
pieces.
reverse()
The Array.reverse() method
reverses the order of the elements of an array and returns the
reversed array. It does this in place; in other words, it doesn’t
create a new array with the elements rearranged but instead
rearranges them in the already existing array. For example, the
following code, which uses the reverse() and join() methods, produces ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access