11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

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

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

Modifying the values of an array is just as easy. To change the second value of the array, just<br />

assign it like this:<br />

myArray[1] = 101;<br />

Of course, when setting array values, you must remember the distinction between reference<br />

and primitive types made in previous chapters. In particular, recall that when you manipulate a<br />

variable that has been set equal to a reference type, it modifies the original value as well. For<br />

example, consider the following:<br />

var firstarray = ["Mars", "Jupiter", "Saturn"]<br />

var secondarray = firstarray;<br />

secondarray[0] = "Neptune";<br />

alert(firstarray);<br />

You‘ll notice, as shown here, that the value in firstArray was changed!<br />

This aspect of reference types is very useful, particularly in the case of parameter passing to<br />

functions.<br />

Removing Array Elements<br />

Array elements can be removed using the delete operator. This operator sets the array element<br />

it is invoked on to undefined but does not change the array‘s length (more on this in a<br />

moment). For example,<br />

var myColors = ["red", "green", "blue"];<br />

delete myColors[1];<br />

alert("<strong>The</strong> value of myColors[1] is: " + myColors[1]);<br />

results in

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

Saved successfully!

Ooh no, something went wrong!