Java Reference
In-Depth Information
var pizzas = ["Margherita", "Mushroom", "Spinach &
Rocket",
"Pineapple & Sweetcorn"];
<< ["Margherita", "Mushroom", "Spinach & Rocket",
"Pineapple &
Sweetcorn"];
You don't even have to use the same types of items inside an array. This array contains
each of the five different types of primitive values, as well as an empty object:
mixedArray = [null, 1, "two", true, undefined, {} ];
<< [null, 1, "two", true, undefined, {}]
Removing Values from Arrays
Donatello can remove a pizza from a box, by opening it and taking the pizza out, leaving
the box empty. The delete operator does the same task and will remove an item from an
array:
delete pizzas[3];
<< true
If we look at the pizzas array, we can see that the fourth entry (with an index of 3 ) has
indeed been removed ... but it has been replaced by a value of undefined (as if the box
was empty):
pizzas;
<< ["Margherita", "Mushroom", "Spinach & Rocket",
undefined]
Watch out for this as it can even trip up experienced programmers. The value that was
in position 3 (" Pineapple & Sweetcorn ") has been deleted from the array, but the
space that it occupied is still there and contains a value of undefined . Remember that
Donatello only removed the pizza from the box and didn't remove the box completely. This
means that the array still has the same number of elements and the position can still be ref-
erenced as an index, but it will just return undefined :
Search WWH ::




Custom Search