06.07.2017 Views

Mastering JavaScript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

The concat() method joins two arrays and returns a new array:<br />

var myArray = new Array("33", "44", "55");<br />

myArray = myArray.concat("3", "2", "1");<br />

console.log(myArray);<br />

// ["33", "44", "55", "3", "2", "1"]<br />

The join() method joins all the elements of an array into a string. This can be useful<br />

while processing a list. The default delimiter is a comma (,):<br />

var myArray = new Array('Red','Blue','Yellow');<br />

var list = myArray.join(" ~ ");<br />

console.log(list);<br />

//"Red ~ Blue ~ Yellow"<br />

The pop() method removes the last element from an array and returns that element.<br />

This is analogous to the pop() method of a stack:<br />

var myArray = new Array("1", "2", "3");<br />

var last = myArray.pop();<br />

// myArray = ["1", "2"], last = "3"<br />

The push() method adds one or more elements to the end of an array and returns<br />

the resulting length of the array:<br />

var myArray = new Array("1", "2");<br />

myArray.push("3");<br />

// myArray = ["1", "2", "3"]<br />

The shift() method removes the first element from an array and returns that<br />

element:<br />

var myArray = new Array ("1", "2", "3");<br />

var first = myArray.shift();<br />

// myArray = ["2", "3"], first = "1"<br />

The unshift() method adds one or more elements to the front of an array and<br />

returns the new length of the array:<br />

var myArray = new Array ("1", "2", "3");<br />

myArray.unshift("4", "5");<br />

// myArray = ["4", "5", "1", "2", "3"]<br />

The reverse() method reverses or transposes the elements of an array—the first<br />

array element becomes the last and the last becomes the first:<br />

var myArray = new Array ("1", "2", "3");<br />

myArray.reverse();<br />

// transposes the array so that myArray = [ "3", "2", "1" ]<br />

[ 89 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!