The notes found on these pages all reference the textbook given above.
Unless otherwise stated, the citation of the information on these web pages
is that text.
Creating Arrays
- var empty =[];
- var primes = [2, 3, 5, 7, 11];
- var a = new Array();
- var a = new Array(10); //a.length is 10
- var a = new Array(5, 4, 3, 2, 1, "testing", "testing");
Reading and Writing Array Elements
- value = a[0];
- a[1] = 3.14;
- i = 2;
- a[i] = 3;
a[i+1]="hello";
a[a[i]]=a[0];
- Indices
- a["10"] // is the 11th element
a[1.00] // is same as a[1]
a[a.length] // is undefined
Adding and deleting array elements
- As above
- a=[] //empty array
a[0] = "hello" //adds an element
- using push
- a=[] //empty array
a.push("hello") //adds an element
- using delete to delete item at index number
- a=[1,2,3] //array of 3 elements
delete a[1]; //deletes element at index 1
1 in a // returns false. no element defined at index 1
a.length // still 3. Just undefined element
Iterating Arrays
Array of images
- Arrays use [ ]
- document.images[ ]
- Code used on images.html page
- for (i=0; i<15; i++)
document.write ("Image " + i + " is " + document.images[i].src + "
");
- OR you can set up the array using Javascript
imgarr=new Array(9); // array of 9 elements
imgarr[0]=new Image; //new image
imgarr[0].src="picnic02-1.jpg";
- OR use a loop to generate the pictures since they are
similarly named
imgarr=new Array(9);
for (i=0;i<8;i++){
imgarr[i]=new Image;
imgarr[i].src="picnic02-"+(i+1)+".jpg";
}